Here's a small snippet of my code. The purpose of the snippet you see here is twofold 1)present a menu -- that's the menu() function, and 2) prompt the user to make a selection and store it -- selection().
selection() starts on line 18.
I'm trying to declare some variables that will be local to my menu() and selection() functions, but I get errors like this, for everything I try to declare:
warning: unused variable 's' (it gave this for all my 'char' variables) or error: 'snack_selection' was not declared in this scope (it gave this for the two 'int' variables I tried to declare)
but how can that be? I don't know what I'm missing.
patient feedback is appreciated.
int menu(void)
{
cout << "Available snacks to select from: " << endl //presents menu
<< " " << setw(5) << "P - Potato Chips" << setw(9) << "$1.25"
<< endl //prompts user to make a selection (until it is valid)
<< " " << setw(5) << "S - Snickers Bar" << setw(9) << "$1.35"
<< endl
<< " " << setw(5) << "T - Pop Tart" << setw(13) << "$0.95"
<< endl
<< " " << setw(5) << "C - Cookies" << setw(14) << "$1.50"
<< endl
<< " " << setw(5) << "B - Brownie" << setw(14) << "$1.75"
<< endl
<< " " << setw(5) << "N - Nuts" << setw(17) << "$1.40"
<< endl;
selection();
return 0;
}
int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection
{
int potato_chip_price = 125, snickers_bar_price = 135, pop_tart_price = 95, cookies_price = 150, brownie_price = 175, nuts_price = 140;
char snack_selection = 0; //condition for snack_selection switch case statement
char P, S, T, C, B, N; //the char variables needed for the snack_selection
cout << "Please enter the letter labeling your snack selection: ";
cin >> snack_selection;
cout << endl;
switch(std::toupper(snack_selection))
{ //beginning of switch statement (goes through all the "legal" options so there is an "illegal" option
case 'P': price = potato_chip_price; //setting it up this way stores the value of price with the value of potato_chip_price. if the other way around, pop_tart_price would become 0 since that is what price is set to.
break;
case 'S': price = snickers_bar_price; //stores the snack selection price in CENTS to the int price variable needed for the next function's argument
break;
case 'T': price = pop_tart_price;
break;
case 'C': price = cookies_price;
break;
case 'B': price = brownie_price;
break;
case 'N': price = nuts_price;
break;
default:
cout << "Invalid selection!";
menu();
} //end of switch statement
return(price); //needs to return some kind of value bc it isn't a void function, and compiler will give a warning
}
full code here with superficial info removed:
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
//Variable Declarations
int price = 0, total_paid = 0, total_price = 0; //arguments for the three functions in the program.need to be global
//Function Declarations:
int menu(void);
// presents menu
int selection(void);
//prompts user to make selection and stores it
int accept_money(int price);
//tells user what money it accepts and the label for each type
//1. repeats back what cost is
//2. tells what has been paid
//3. allows user to input money by payment selection here
int compute_change(int total_paid, int total_price);
//1. tells user what total amt was paid
//2. computes change
//3. tells user what change will be
//4. ask user if he or she would like another purchase (if statement. if y, send back to int menu(). if no, thank for purchase message.)
int main()
{
cout << "Welcome to the snack vending machine" << endl << endl;
menu();
accept_money(price);
compute_change(total_paid, total_price);
return 0;
}
//Function Definitions
int menu(void)
{
cout << "Available snacks to select from: " << endl //presents menu
<< " " << setw(5) << "P - Potato Chips" << setw(9) << "$1.25"
<< endl //prompts user to make a selection (until it is valid)
<< " " << setw(5) << "S - Snickers Bar" << setw(9) << "$1.35"
<< endl
<< " " << setw(5) << "T - Pop Tart" << setw(13) << "$0.95" <<
endl
<< " " << setw(5) << "C - Cookies" << setw(14) << "$1.50" <<
endl
<< " " << setw(5) << "B - Brownie" << setw(14) << "$1.75" <<
endl
<< " " << setw(5) << "N - Nuts" << setw(17) << "$1.40" <<
endl;
selection();
return 0;
}
int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection
{
int potato_chip_price = 125, snickers_bar_price = 135, pop_tart_price = 95, cookies_price = 150, brownie_price = 175, nuts_price = 140;
char snack_selection = 0; //condition for snack_selection switch case statement
char P, S, T, C, B, N; //the char variables needed for the snack_selection switch case statement (can't be inside function, needs to be global
cout << "Please enter the letter labeling your snack selection: ";
cin >> snack_selection;
cout << endl;
switch ( std::toupper(snack_selection) )
{ //beginning of switch statement (goes through all the "legal" options so there is an "illegal" option
case 'P': price = potato_chip_price; //setting it up this way stores the value of price with the value of potato_chip_price. if the other way around, pop_tart_price would become 0 since that is what price is set to.
break;
case 'S': price = snickers_bar_price; //stores the snack selection price in CENTS to the int price variable needed for the next function's argument
break;
case 'T': price = pop_tart_price;
break;
case 'C': price = cookies_price;
break;
case 'B': price = brownie_price;
break;
case 'N': price = nuts_price;
break;
default:
cout << "Invalid selection!";
menu();
} //end of switch statement
return(price); //needs to return some kind of value bc it isn't a void function, and compiler will give a warning
}
int accept_money(int price)
{
int nickel = 5, quarter = 25, dollar = 100;
char money_selection = 0; //condition for money_selection switch case statement
char n, q, d; //the char variables needed for the money_selection switch case statement
cout << "Money accepted by the machine:\n"
<< " " << setw(5) << "N - Nickel" << endl
<< " " << setw(5) << "Q - Quarter" << endl
<< " " << setw(5) << "D - Dollar" << endl << endl;
do
{
cout << "Your selected snack item cost: " << price << " CENTS" << endl
<< "Your total inserted: " << total_paid << " CENTS" << endl
<< "Insert amount (enter letter of choice): ";
cin >> money_selection;
cout << endl;
switch ( std::tolower(money_selection) )
{
case 'n': total_paid = nickel + total_paid;
cout << endl;
break;
case 'q': total_paid = quarter + total_paid;
cout << endl;
break;
case 'd': total_paid = dollar + total_paid;
cout << endl;
break;
default:
cout << money_selection << " is not recognized as a coin." << endl << endl;
}
} while ( total_paid < price );
return total_paid; //this function needs to return updated value, or any changes made to it will remain local to the function
}
int compute_change(int total_paid, int total_price)
{
char continue_purchase = 0;
char y;
int change = total_paid - price; //has to have somewhere to be stored in this function to work
int amt_inserted = total_paid; //was not using updated value of total_paid from previous fnction, so had to save it to a variable local to this one from the global variable
cout << "Your total inserted: " << amt_inserted << " CENTS" << endl
<< "Dispensing change: " << change << setw(5) << " CENTS" << endl
<< "Would you care to make another purchase (Y/N): ";
cin >> continue_purchase;
switch ( std::tolower(continue_purchase) )
{
case 'y': total_paid = 0;
price = 0;
total_price = 0;
cout << endl << endl;
menu();
break;
case 'n': cout << "Thank you and enjoy your purchase!";
break;
default:
cout << "Invalid Selection" << endl << endl;
compute_change(total_paid, total_price);
}
return 0;
}