0

this loop is running more times then i want to and when the user puts in the wrong number how do i print out an error.

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

using namespace std;

int main()
{

string answer;
int soda;

do
 {
           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 $ " << endl;
    if(soda == 2)
    cout << "You purchased Water for a cost of $" << endl;
    if(soda == 3)
    cout << "You purchased Sprite for a cost of $" << endl;
    if(soda == 4)
    cout << "You purchased Dr. Pepper for a cost of $" << endl;
    if(soda == 5)
    cout << "You purchased Lemonade for a cost of $" << endl;

}     
    while (soda != 6);





}
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by wids101
0

Well for one, you are asking the loop to stop ONLY if the user enters 6. Perhaps you want it to break out when an option is chosen?

Also have you considered using a switch case statement? Another thing is that if you want it to break out of the loop whenever an option is chosen, do:

if (sode == 1)
{
    cout<<"You paid for......"<<endl;
    break;
} 

//For the error thing:

if ((soda < 1) && (soda > 6))
    cout<<"Error.. Invalid option"<<endl;

Here's an example of the case:

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

using namespace std;

int main()
{

    string answer;
    int soda;

    do
    {
        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;
        cin.ignore();

        switch(soda)
        {
            case 1:
                cout << "You purchased Coke for a cost of $ " << endl;
            break;

            case 2:
                cout << "You purchased Water for a cost of $" << endl;
            break;

            case 3:
                cout << "You purchased Sprite for a cost of $" << endl;
            break;

            case 4:
                cout << "You purchased Dr. Pepper for a cost of $" << endl;
            break;

            case 5:
                cout << "You purchased Lemonade for a cost of $" << endl;
            break;

            default: cout<<"Error Invalid Option."<<endl; break;
        }
    } while (soda != 6);
}

Edited by triumphost

0

alright, i wanted to know if the user inputted 5. after that i want the program to end.

0

You are using a do ... while, so your message will always appear at least once, but then if you choose a "correct" number, the loop goes again, cause it will only stop if you press 6.
You could change your while condition to

while(soda < 1 || soda > 5);

In this way, the program should stop wether you press 1,2,...,6.

For the error, you may just use

if(soda < 1 || soda > 6)
cout << ("Error: you pressed an invalid option");

Hope it's useful.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.