Nick5227 - 23 days ago 5x
C++ Question

# Trouble with do-while loop

I am confused about how to make this do while loop work correctly. The goal is to get the code to loop back to the "main menu" and allow you to select another item and add it to your "cart" for lack of a better word. The problem is that my code right now will only give you the subtotal for the first item selected and then exit without going back to the main menu, I would like to be able to loop into the menu until exit is selected and THEN calculate the total. Any help is appreciated!

``````    int main()
{
int dice;                           //Amount of dice
int bobble;                         //Amount of bobble heads
int selection;                      //Selection

string code;                        //User input coupon code

double diceTotal;                   //Subtotal for dice
double bobbleTotal;                 //Subtotal for bobble heads
double total;                       //Total cost of purchase

const double DICE = 6.25;           //Dice cost
const double BOBBLE1 = 16.99;       //1-5 Bob. Head cost
const double BOBBLE2 = 14.99;       //6-10 Bob. Head cost
const double BOBBLE3 = 12.99;       //11+ Bob. Head cost
const string COUPON = "beads1";     //Coupon code

cout << fixed << setprecision(2);

//Welcome
do
{
cout << setw(50) << "Welcome to DecoCar!" << endl;
cout << setw(49) << "Nick Wester, Owner" << endl;
cout << "Our inventory: " << endl;
cout << "1. Fuzzy Dice" << endl;
cout << "2. Mardi Gras Beads" << endl;
cout << "3. Bobble Heads" << endl;
cout << "4. Exit" << endl << endl;
cout << "Please make a selection: ";
cin >> selection;
}
while (selection <= 0 || selection >= 5);
{
if (selection == 1)
{
cout << "How many Fuzzy Dice would you like to buy? ";
cin >> dice;
while (dice < 0)
{
cout << "How many Fuzzy Dice would you like to buy? ";
cin >> dice;
}
diceTotal = dice*DICE;
cout << "Your subtotal for the Fuzzy Dice: \$" << diceTotal << endl;
}
else if (selection == 2)
{
cout << "How many sets of Mardi Gras beasd would you like to buy? ";
cin >> code;
{
cout << "Valid code entered" << endl;
}
else
{
}
}
else if (selection == 3)
{
cout << "How many Bobble Heads would you like to buy? ";
cin >> bobble;
if (bobble >= 1 && bobble <= 5)
{
bobbleTotal = bobble*BOBBLE1;
cout << "Your subtotal for Bobble Heads: \$" << bobbleTotal << endl;
}
else if (bobble >= 6 && bobble <= 10)
{
bobbleTotal = bobble*BOBBLE2;
cout << "Your subtotal for Bobble Heads: \$" << bobbleTotal << endl;
}
else if (bobble >= 11)
{
bobbleTotal = bobble*BOBBLE3;
cout << "Your subtotal for Bobble Heads: \$" << bobbleTotal << endl;
}
}
}
}
``````

It looks like your code may need some redesigning:

Try this pseudo-code:

``````do
{

Switch(selection)
{
Case 1: ask for quantity and break;
Case 2: same here
Case 3: same
Case 4: same
Default : break
}

Calculate the subtotal based on above case selected.
}while(selection is greater than 0 and less than 5);

Finally print the total amount
``````
Source (Stackoverflow)