How to get switch to go back to original input instead of previous?
Feb 24, 2017 at 3:58am UTC
Hi. I'm still pretty new at coding and this is my first big project for my class.
I have to make a project for a store where the user can order things off the menu and then it makes a receipt. I've been able to fix every issue except this one. I've been stuck for hours now.
I want it to ask if the user wants to order anything else, and if yes, it loops back to the original "What would you like to order?". But instead, it keeps going back to "How many vanilla cake slices would you like?: " and such.
This is a simplified version of my code, since the actual one is super long and I'm pretty sure I can fix the rest of it if I can fix this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
system("clear" );
cout << fixed << setprecision(2);
const double SliceP = 2.50;
const double MiniP = 6.00;
char order, order2, order3;
int Slice = 0, Mini = 0;
int VanST = 0, VanMT = 0;
int ChST = 0, ChMT = 0;
int MarST = 0, MarMT = 0;
int VanS = 0, VanM = 0;
int ChS = 0, ChM = 0;
int MarS = 0, MarM = 0;
cout << "\t\tWelcome to Ann's Bakery!" ;
cout << "\n\n\t\t\tMenu" ;
cout << "\n\n\t===================================" ;
cout << "\n\t\t\tCakes" ;
cout << "\n\t-----------------------------------" ;
cout << "\n\tFlavors: Vanilla, Chocolate, Marble" ;
cout << "\n\t-----------------------------------" ;
cout << "\n\tSlice\t\t\t$" << SliceP;
cout << "\n\tMini (4 in)\t\t$" << MiniP;
cout << "\n\t===================================\n\n" ;
cout << "Would you like to order? (Y = yes; N = no): " ;
cin >> order;
while ((order != 'n' ) && (order != 'N' ))
{
switch (order)
{case 'y' :
case 'Y' :
cout << "\nWhat would you like to order?" ;
cout << "\nEnter C for Cake" ;
cout << "\nEnter P for Pastry" ;
cout << endl << endl;
cin >> order2;
while ((order2 != 'p' ) && (order2 != 'P' ) && (order2 != 'c' ) && (order2 != 'C' ))
{
cout << "Please enter C or P: " ;
cin >> order2;
}
while (((order2 == 'c' ) || (order2 == 'C' ) || (order2 == 'p' ) || (order2 == 'P' )) && ((order == 'y' ) || (order == 'Y' )))
{
switch (order2)
{case 'c' :
case 'C' :
cout << "\nWhat size would you like?" ;
cout << "\nEnter S for Slice" ;
cout << "\nEnter M for Mini" ;
cout << endl << endl;
cin >> order3;
while ((order3 != 's' ) && (order3 != 'S' ) && (order3 != 'm' ) && (order3 != 'M' ))
{
cout << "Please enter S or M: " ;
cin >> order3;
}
while (((order3 == 's' ) || (order3 == 'S' ) || (order3 == 'm' ) || (order3 == 'M' ))
&& ((order == 'y' ) || (order == 'Y' )))
{
switch (order3)
{case 'S' :
case 's' :
cout << "\nHow many vanilla cake slices would you like?: " ;
cin >> VanS;
VanST = VanST + VanS;
Slice = Slice + VanS;
cout << "How many chocolate cake slices would you like?: " ;
cin >> ChS;
ChST = ChST + ChS;
Slice = Slice + ChS;
cout << "How many marble cake slices would you like?: " ;
cin >> MarS;
MarST = MarST + MarS;
Slice = Slice + MarS;
cout << "Would you like to order more? (Y = yes; N = no): " ;
cin >> order;
break ;
default :
cout << "\nHow many mini vanilla cakes would you like?: " ;
cin >> VanM;
VanMT = VanMT + VanM;
Mini = Mini + VanM;
cout << "How many mini chocolate cakes would you like?: " ;
cin >> ChM;
ChMT = ChMT + ChM;
Mini = Mini + ChM;
cout << "How many mini marble cakes would you like?: " ;
cin >> MarM;
MarMT = MarMT + MarM;
Mini = Mini + MarM;
cout << "Would you like to order more? (Y = yes; N = no): " ;
cin >> order;
}
}
break ;
default :
cout << "Removed to cut down code" ;
order = 'n' ;
}
}
break ;
default :
while ((order != 'y' ) && (order != 'Y' ) && (order != 'n' ) && (order != 'N' ))
{
cout << "Please enter Y or N: " ;
cin >> order;
}
}
}
double SlicePF = Slice*SliceP;
double MiniPF = Mini*MiniP;
cout << "\n\n\t==================================" ;
cout << "\n\t\t\tReceipt" ;
cout << "\n\t==================================" ;
cout << "\n\t\t\tCakes" ;
cout << "\n\t----------------------------------" ;
cout << "\n\t" << Slice << " Slice(s)\t\t$" << SlicePF;
cout << "\n\t\t" << VanST << " Vanilla" ;
cout << "\n\t\t" << ChST << " Chocolate" ;
cout << "\n\t\t" << MarST << " Marble" ;
cout << "\n\t" << Mini << " Mini Cake(s)\t\t$" << MiniPF;
cout << "\n\t\t" << VanMT << " Vanilla" ;
cout << "\n\t\t" << ChMT << " Chocolate" ;
cout << "\n\t\t" << MarMT << " Marble" ;
cout << "\n\t==================================" ;
cout << "\n\tTotal number of items: " << Slice+Mini;
cout << "\n\tTotal price: $" << SlicePF+MiniPF;
cout << endl;
cout << "\n\t\tThank you! Enjoy!" ;
cout << "\n\t==================================" ;
cout << endl;
return 0;
}
Feb 24, 2017 at 5:07am UTC
did you actually change order3 before you evaluate it again in the switch by reading it in from the user or something? It looks like that inner while loop will keep evaluating order3 but never changes it...?
Feb 24, 2017 at 11:37am UTC
At line 72, change while
to if
.
Feb 24, 2017 at 10:49pm UTC
Thank you so, so much dyhayden. After trying to fix this for hours, I never would have though changing some while statements to ifs would completely fix my problem. Thank you so much.
Topic archived. No new replies allowed.