I have an outer loop(do while) and inner loop(while), the program runs through t
ID: 3626859 • Letter: I
Question
I have an outer loop(do while) and inner loop(while), the program runs through the outer loop and then the inner loop fine but once it gets back out to the outer loop, it does not seem to read the user's input correctly. So basically, the program is able to only go through the outerloop then inner loop successfully once. Why won't it go through the entire outer loop again? I know the codes work because it would output "invalid size" if I put an illegal character.do
{
cout << " Would you like a [S]small , [M]medium, [L]large pizza or [Q]quit?" <<endl;
cin >> pizza_size;
if ((pizza_size[0] == 'S') || (pizza_size[0] == 's'))
{
order.SetSize(0);
}
else
cout<< "invalid size" << endl;
etc etc
if (pizza_size[0] != 'Q' && pizza_size[0] != 'q')
{
while (choice != 0)
{
cout << " Selection? ";
cin >> input;
choice = atoi(input);
if (!isdigit(input[0]))
choice = -1;
order.AddTopping(choice);
}
}
} while (((pizza_size[0] != 'Q') && (pizza_size[0] != 'q'));
Explanation / Answer
please rate - thanks
the problem is you are using pizza_size as an array, and not as at array
my guess is it isn't an array so change the code to this
do
{
cout << " Would you like a [S]small , [M]medium, [L]large pizza or [Q]quit?" <<endl;
cin >> pizza_size;
if ((pizza_size == 'S') || (pizza_size== 's'))
{
order.SetSize(0);
}
else
cout<< "invalid size" << endl;
etc etc
if (pizza_size!= 'Q' && pizza_size != 'q')
{
while (choice != 0)
{
cout << " Selection? ";
cin >> input;
choice = atoi(input);
if (!isdigit(input[0]))
choice = -1;
order.AddTopping(choice);
}
}
} while (((pizza_size != 'Q') && (pizza_size != 'q'));
--------------------------------------------------------------------
if it is an array change the code to
do
{
cout << " Would you like a [S]small , [M]medium, [L]large pizza or [Q]quit?" <<endl;
cin >> pizza_size[0];
if ((pizza_size[0] == 'S') || (pizza_size[0] == 's'))
{
order.SetSize(0);
}
else
cout<< "invalid size" << endl;
etc etc
if (pizza_size[0] != 'Q' && pizza_size[0] != 'q')
{
while (choice != 0)
{
cout << " Selection? ";
cin >> input;
choice = atoi(input);
if (!isdigit(input[0]))
choice = -1;
order.AddTopping(choice);
}
}
} while (((pizza_size[0] != 'Q') && (pizza_size[0] != 'q'));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.