Practicing my if/else functions with a simple number program that asks the user to input and carry out functions to a chosen number. If the user inputs the wrong answer or an invalid number I would like the code to be able to be restarted. Is there any way of doing this?
#include <iostream>
usingnamespace std;
void correct() //Function to display text if correct answer inputted
{
cout << "Correct\n" << endl;
}
void incorrect()
{
cout << "Incorrect, press r to start again" << endl; //Function to display text if incorrect answer inputted
//some code to make program restart if r is pressed
}
int main()
{
int x;
cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
cin >> x;
if (x > 0 && x < 11)
{
cout << "Thank you\n" << endl;
}
else
{
cout << "Number invalid, press r to start again\n" << endl;
return 0;
}
cout << "Please double your number" << endl;
int y;
cin >> y;
if (y == x*2)
{
correct();
}
else
{
incorrect();
return 0; //Ends program
}
cout << "Please add 2 to your number" << endl;
int z;
cin >> z;
if (z == y + 2)
{
correct();
}
else
{
incorrect();
return 0; //Ends program
}
return 0;
}
int main()
{
while (true) // or whatever other condition, if you want to limit it)
{
//stuff you already have
...
else
{
incorrect();
//instead of returning...
continue; //makes it start back at the top of the while loop
}
}
}
The ellipsis was supposed to mean the rest of the code.
As in, wrap everything you currently have in main() around a while loop, but change the returns to continues.
1 2 3 4 5 6 7 8 9 10
if (y == x*2)
{
correct();
}
else
{
incorrect();
continue;
}
//do this for the other places you want to "restart" instead of return as well
Edit: If you want a condition in your while loop, it would be a condition that, if evaluated to false, would make the program end. You could have a "num_tries" that gets incremented, and it num_tries goes above 10 or something, it ends the loop.
int main()
{
while (true) {
int x;
cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
cin >> x;
if (x > 0 && x < 11)
cout << "Thank you\n" << endl;
else
{
cout << "Number invalid, press r to start again\n" << endl;
continue; //restarts to beginning of while loop
}
cout << "Please double your number" << endl;
int y;
cin >> y;
if (y == x*2)
{
correct();
}
else
{
incorrect();
continue; //restarts to beginning of while loop
}
cout << "Please add 2 to your number" << endl;
int z;
cin >> z;
if (z == y + 2)
{
correct();
}
else
{
incorrect();
continue; //restarts to beginning of while loop
}
//If this point is reached, it means the person followed the directions correctly.
break; //ends loop, allows program to return and exit.
}
return 0;
}
Right that all works now (I included a break; after the last if to end the program if the user gets to the last function correctly), thanks for the help. Just out of interest (not that you'd need/want to do this but) would it be possible to make the program restart if the user inputs a character?
Ahh thank you very much. In what way could it be better? I'm a total beginner so if you could suggest a general solution kind of thing I could work through to help me improve that would be fab, if not maybe some specific pointers?
#include <iostream>
usingnamespace std;
int z;
void correct() //Function to display text if correct answer inputted
{
cout << "Correct\n" << endl;
}
void incorrect()
{
cout << "Incorrect, press r to start again" << endl; //Function to display text if incorrect answer inputted
//some code to make program restart if r is pressed
}
int main()
{
int x;
do{
cout << "Enter am integer between 1 and 10 (inclusive)" << endl;
cin >> x;
}
while(x<1 || x>10); // here's your problem i changed it into do while loop if the input
// is wrong the program will be restarted
cout << "Please double your number" << endl;
int y;
cin >> y;
if (y == x*2)
{
correct();
}
else
{
incorrect();
return 0; //Ends program
}
cout << "Please add 2 to your number" << endl;
int z;
cin >> z;
if (z == y + 2)
{
correct();
}
else
{
incorrect();
return 0; //Ends program
}
return 0;
}