#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

int soda;


           cout << "Choose a drink or 6 to quit:"; << endl;
           cout << "1. Coke       $1.00"; << endl;
           cout << "2. Water      Free"; << endl;
           cout << "3. Sprite     $1.25"; << endl;
           cout << "4. Dr. Pepper $1.75"; << endl;
           cout << "5. Lemonade   $.60"; << endl;
           cout << "6. Quit! \n"; << endl;
           cin >> soda;


    if(soda == 1)
    cout << "You purchased Coke for a cost of $";
    if(soda == 2)
    cout << "You purchased Water for a cost of $";
    if(soda == 3)
    cout << "You purchased Sprite for a cost of $";
    if(soda == 4)
    cout << "You purchased Dr. Pepper for a cost of $";
    if(soda == 5)
    cout << "You purchased Lemonade for a cost of $";
    if(soda == 6)




    return 0;



}

i need help with this code and i'm stuck. i need wrtie a program that displays a menu and alllows them to enter a beverage by enter 1-5 while 6 to quit. i know i need a loop somewhere and this is only for one item at a time. thank you.

Recommended Answers

All 4 Replies

  1. Use << endl output manipulators after each output line so that the output is flushed to the display, and it appears on its own line.
  2. The loop is simple:

    while (soda != 6)
    {
    // 1. display menu
    // 2. decode input to decide what to do.
    // 3. done!
    }

Look at the switch() statement - that is better (in my opinion) than if/else if/else logic to handle this sort of situation.

So, keep at it - you are making progress!

alright thanks! if i want to display the price also do i need to declare something else and do math or no and to stop the program i need a system("cls"0); after the pruchase?

I would suggest you do it with a string instead of that int as the chosen variable, in order to rule out possible errors. I also would suggest using a for loop, and inside of it to make your validation via the if-else clauses. Here's a quick example of how you can do it:

#include <string>
#include <iostream>
using namespace std;
int main(){
    string answer;
    // menu over here.
    cout<<"1. Hello.\n"
        <<"2. Bye.\n";
    for (;;){
        cout<<"\\\\: ";
        cin>>answer;
        if (answer=="1" || answer=="1.") cout<<"Hello.\n";
        else if (answer=="2" || answer=="2.") {
            cout<<"Bye.\n";
            break;
        else cout<<"Invalid command.\n";
    }
    return (0);
}

Notice that I used a string answer as a variable where I store my answer. If I put an int, and someone misstypes and inserts a character, different from a digit, the program would crash. So, in order to avoid that, I put a string object, in which I would store the answer, than via the if-else clauses I would "filter" it. If no condition is fulfilled, the message of Invalid command will show on the screen, and again, the prompter asking for another input. So, if someone misstypes and inserts a character now it would only show the invalid command message, and than the prompter would ask for another input. The exit part is done, as you can see in the 2nd option, via the break; statement. Remember, the for (;;) is an infinite loop, and by that it will always run the code inside of it, so we need something to "break" it. This is done easily with the break; statement, which will break that infinite loop, and continue with the program.
If you still need further explanations, don't hesitate to ask.

alright thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.