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?
//ultimate MATH PROGRAM
#include <iostream>
#include <string>
usingnamespace 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?
}
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.
@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.
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
} elseif (r == 'N') {
//Code Path 2
} else { //Ir r neither Y or N
//Code Path 3
}