This is phase three of the project I asked about yesterday:
It's the skeleton of a program without the functions added--yet. Please notice that when the option sequence of 1, N, 2 and option sequence 2, N, 2 is used, the loop sticks until the option sequence 1, Y, Y or 1, N, 1 is used. Both "if" statements look the same to me.
I am aware the set-up is not a finished product. There are holes in it for six functions and a thorough style editing. (The gratuitous comments are to help me keep track of future additions) However, you should be able to follow what I am attempting to do.
/*Description: This program uses functions from projects #6 pg298 and #7
pg299 to give the user a choice of converting metric to American or vice
versa. User has integer option (1 or 2) to choose conversion options using
if-else statements. Each branch of the if-else statement will be a function
call (2) with similar definitions to #6 and #7. Include loop to repeat
computation until the user chooses to quit.*/
#include <iostream>
usingnamespace std;
int main()
{/*main*/
int choice;
char ans;
cout << "This program converts metric measurements to American or vice versa." << endl << endl;
do /*1*/
{
do /*2*/
{
do /*3*/
{
do /*4*/
{
cout << "Enter 1 for American to metric or 2 for converting metric to American: ";
cin >> choice;
cout << endl;
}while (choice > 2); /*4 does not allow user to enter anything other than 1 or 2*/
if (choice == 1) /*#1*/
{/*if#1*/
cout << "You chose American to metric conversion." << endl <<endl;
cout << "Is this correct? Enter Y to continue or N to change entry: ";
cin >> ans; /*Allows user to correct entry or continue program*/
cout << endl;
} /*if#1*/
}while (ans == 'N' || ans == 'n');/*2*/
if (choice == 2) /*#2*/
{ /*if#2*/
cout << "You chose metric to American conversion." << endl <<endl;
cout << "Is this correct? Enter Y to continue or N to change entry: ";
cin >> ans; /*Allows user to correct entry or continue program*/
cout << endl;
} /*if#2*/
}while (ans == 'N' || ans == 'n'); /*3*/
cout << "Would you like to choose a new conversion?" << endl << endl;
cout << "Enter Y to repeat or N to exit the program: ";
cin >> ans;
cout << endl;
}while (ans == 'Y' || ans == 'y'); /*1*/
return 0;
}/*main*/
What do you see? Is the mistake somewhere other than the "if" statements? I'm hoping some fresh eyes will shed some light on the mistake I've been staring at and not seeing.
Your loops are nested weirdly. The user enters 1, getting into if statement 1. They put in N, looping on while 2. Now, if they enter choice 2, if 1 will be skipped and while 2 will loop bringing them back to the beginning.
Also, the user could enter 0 for choice and your code doesn't even try to handle that.
To fix the problem, you could do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
dodo
ask for choice
while choice is not valid
if choice is 1
confirm choice 1 text
elseif choice is 2
confirm choice 2 text
end
ask for confirm value
while not confirmed
if choice is 1
do choice 1
elseif choice is 2
do choice 2