How should I edit my program so if I enter a # bigger than 80, I have to enter another number ("input") and if this "input" is equal to/less than 80, it gives me one of the 3 following statements?
usingnamespace std;
int main( )
{
int Input;
int x;
again: //label is here
cerr << "Input:" << endl;
cin >> Input;
if (Input/2 <= 29.5)
{
cout << "You got a F" << endl;
}
if (Input/2 > 29.5 && Input/2 <= 34.5)
{
cout << "You got a D" << endl;
}
if (Input > 69 && Input <= 79)
{
cout << "You got a C" << endl;
}
if (Input > 80)
{
cout << "The grade you entered cant be above 80, try again" << endl;
goto again;// if the value is greater than 80 it will jump to the label
}
return 0;
}
Please don't do like programmer007 gave as answer:
just to make the work easy use a goto statement
don't ever use goto. They are not sign of a good programming. Sometimes they can be very tempting but don't use them.
As MiiNiPaa said
Nothing in your code says "return back to the beginning"
the main function is executed only once. It does not re-loop by itself.
As boothkeeper said use a while loop instead.
but he didn't correctlly use the if/else conditions:
1 2 3 4 5 6 7 8 9 10 11 12
bool validGrade=falsewhile(!validGrade)
{
validGrade=true;
//read the input here
//do the if conditions here
if(Input>80)
{
cout << "The grade you entered can't be above 80, try again" << endl;
validGrade=false; //to return to the begining
}
}