Option 2 Loop Wrong, or Something Else?

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.

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
/*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>
using namespace 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.

Thank you!

K
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
do
    do
        ask for choice
    while choice is not valid

    if choice is 1
       confirm choice 1 text
    else if choice is 2
       confirm choice 2 text
    end

    ask for confirm value
while not confirmed

if choice is 1
    do choice 1
else if choice is 2
    do choice 2

Haven't tested it but I think it'll work.
I think I understand. In my version, option 2 doesn't loop on itself if the user wants to correct the entry. It loops back to the beginning?

Your suggestion also has a better presentation. Thanks for the "less than 1" heads up as well.

Thanks, Zhuge.

K
Zhuge, I'm seeing your intention more clearly now:

If 1 is correct - continue
If 2 is correct - continue

If 1 is wrong
(program "knows") 2 has to be right

If 2 is wrong
(program "knows") 1 has to be right


Awesome.

K
Topic archived. No new replies allowed.