0

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;
}
jodag
  • 19,885
  • 5
  • 47
  • 66
Lee Lemur
  • 31
  • 1
  • 6
  • The code you show never declares a variable named `d`, unused or otherwise. Error messages you cite appear to relate to some code you haven't shown. – Igor Tandetnik Jul 06 '17 at 04:50
  • 1
    You never declared `price` ? – M.M Jul 06 '17 at 04:50
  • sorry. price is declared globally in main (as were all the variables used as arguments for my functions). i may have edited out too much in an attempt to make this more readable. – Lee Lemur Jul 06 '17 at 04:52
  • 'd' was only used as an example of the various letter characters I try to declare. I suppose in this instance that is 'p', 's', 't', etc. sorry, should've been more clear. – Lee Lemur Jul 06 '17 at 04:53
  • 1
    Add prototype of `selection()` before `menu()` or move definition of `selection()` before `menu()`. – Gaurav Sehgal Jul 06 '17 at 04:55
  • Please make a [mcve]. – Yunnosch Jul 06 '17 at 04:56
  • Also, warnings are just that...warnings. Unused variable warning means you declared a variable but never used it. – jodag Jul 06 '17 at 04:57
  • the prototype that I have before main() is: int selection(void); – Lee Lemur Jul 06 '17 at 04:57
  • there's no such thing as "globally in main" – M.M Jul 06 '17 at 04:58
  • 2
    `char P, S, T, C, B, N;` None of these variables are being used. These are variables of type `char` named `P`, `S`, etc... Distinctly different from literal `char` values `'P'`, `'S'`, etc... Removing this line will get rid of some warnings about unused variables. – jodag Jul 06 '17 at 05:01
  • 1
    You need to post the **full** source (this includes any `#include` directives) and the **full** text of error messages **exactly as they appear on your screen**. Use the [edit] link to add necessary information to your question. – n. m. could be an AI Jul 06 '17 at 05:03
  • "sorry. price is declared globally in main" no it isn't. It is declared **locally** in `main` (if it is declared anywhere — your question doesn't contain your actual code so no one can tell anything definite about your actual problem). A variable declared in a function is declared locally, not globally. `main` is no exception. – n. m. could be an AI Jul 06 '17 at 05:09
  • thank you for the clarification about global being outside int main. @jodag, are you saying there is no need to declare the char variables at all? – Lee Lemur Jul 06 '17 at 05:17
  • I would advice to do some research and learn the difference between variable, constant and literal values. – K. Kirsz Jul 06 '17 at 05:19
  • @Lee Lemur That is correct, you aren't using them so you don't need to declare them. `'P'` is a `char` literal. Like `1` is an `int` literal. You wouldn't need to declare `int 1` before using the value `1` in your code would you? – jodag Jul 06 '17 at 05:19
  • Your code compiles fine in VS2015. It has warnings due to unused variables which I mentioned before. Similar issues with `char n, q, d;` and `char y;` – jodag Jul 06 '17 at 05:33
  • Avoid using global variables. – n. m. could be an AI Jul 06 '17 at 05:55

2 Answers2

0

The code compiles fine but contains warnings of unused variables. In 3 places you declare char variables which are unused. Removing these declarations gets rid of the warnings. Here I've posted the modified code.

#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

    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

    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;

    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;
}
jodag
  • 19,885
  • 5
  • 47
  • 66
0

Apart from what everyone said in the comments about not using global variables and other good programming practices, the code itself compiles fine (unless you have -Werror switch in your compiler flags active like me.) BUT with warnings.

You seem to have misunderstood that if you are going to use the string literal 'P' in a case statement, as you are doing in the function int selection(void) , that you have to first declare a variable char P for that. You do NOT have to declare a variable for using those string literals. There is NO connection whatsoever between the variable char P and the string 'P'.

The warnings, come from these lines:

In the function int compute_change(int total_paid, int total_price) remove the following line:

char y;

Then in the function int accept_money(int price) remove the following line:

char n, q, d; //the char variables needed for the money_selection switch case statement

And finally in the function int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection

remove the following line.

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

You would be fine after that.

If you really really want to use those variables, P, S and so on, then you should assign them a value and declare them to be constant. I did one of the cases here, for you to see. You can do the rest along similar lines.

int compute_change(int total_paid, int total_price)
{
    char continue_purchase = 0;
    const char y = '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;
}

But just to make it clear, that variable y could've been named anything- it does not have to be named y to store 'y'. The code would've worked just fine if I replace that line with const char hakunamatata = 'y'

int compute_change(int total_paid, int total_price)
{
    char continue_purchase = 0;
    const char hakunamatata = '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 hakunamatata:
        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;
}

The compiler warns you about unused variables/parameters as it reveals genuine errors of oversight/omission,etc on your part (e.g. in this case as you seem to have misunderstood that declaring a char variable is needed to use a string literal containing the same text as the name of that char variable). See this question for more on that: What are the consequences of ignoring: warning: unused parameter

Hope that helps.

Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43