Program if else hassle

Last question of the day. Whenever i press "Y" and go down path 1 i always get the error message from the "else" section. I want to continue with the program and use the new values entered in path 1, but i want to move where it is. Is this possible?

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
//ultimate MATH PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a, b;
float c;
char r;
string str;
    cout << "two integers" <<endl << "first value" << endl;
    cin >> a;
    cout << "second value" << endl;
    cin >> b;
    cout << "is this correct____Y/N" << endl <<"\t" << a << "\t" << b << endl;

    cin >> r;
        if (r == 'y' || r == 'Y'){ //path 1
            cout << "please enter float/ decimal" << endl;
            cin >> c;
            a=c*2;
            cout << a;
            }

        if (r == 'n'|| r == 'N'){ //path 2
            cout << "restart program";
            }
        else {cout << "failed";}//this keeps showing up even 
                                // though the criteria isn't met

//anyway how do i go about continuing
//the program down here?
}

Program's do what you tell them to do.
I think this is what you want? Adding an additional bit of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
            if (r == 'y' || r == 'Y'){ //path 1
            cout << "please enter float/ decimal" << endl;
            cin >> c;
            a=c*2;
            cout << a;
            }
           else
           {

                if (r == 'n'|| r == 'N'){ //path 2
                cout << "restart program";
               }
               else {cout << "failed";}
          }
You have declared a and b as integers which makes it not work when the user enters a letter. You probably want a and b to be of type char.

To make the program repeat you should use a loop. Put the code that you want to repeat inside the loop. In your program it looks like you want lines 11-15 to repeat.
Last edited on
@Peter87 A and B are integers though. The values I enter for a and b are numbers not characters. R is a user defined character. Else keeps activating after path 1 is complete.
This is your code structure:
1
2
3
4
5
6
7
8
9
10
if(character is Y)
  // Code Path 1
//Note that there is no else branch to this if statement

//----- Code above and below this line has no relation to each other ----

if(character is N)
    // Code Path 2
else //Belongs to previous if 2 lines above
    // Code Path 3 
See what happens when you enter Y: First if statement is executed. It is true so Code Path 1 is executed.
Then second if statement is executed. It is false so control is given to corresponding else and Code Path 3 is executed.

You want either if-else chain:
1
2
3
4
5
6
7
8
r = toupper(r);
if(r == 'Y') {
    //Code Path 1
} else if (r == 'N') {
    //Code Path 2
} else { //Ir r neither Y or N
    //Code Path 3
}
Or switch:
1
2
3
4
5
6
7
8
9
10
switch (r) {
  case 'Y':  case 'y': {
    //Code Path 1
    } break;
  case 'N':  case 'n': {
    //Code Path 2
    } break;
  default:
    //Code Path 3
}
Last edited on
Topic archived. No new replies allowed.