please use [ code] [ /code] tags around your code so it is more legible.
1 2 3 4 5 6 7 8 9
|
if (pattern == 2)
cout << " Good Job, It is 2" << endl;
else
{
cout << " Wrong Restart the program you idiot." << endl << endl;
cout << " Oh! I don't know i's input" << endl;
cout << " If the pattern countines What will the variable i = ";
cin >> i;
}
|
You need to group the four statements following the else in a block, otherwise you're compiler reads the program like this.
Start IF
if pattern == 2, carry out the statement,
End IF
Start Else
else, say "Wrong restart the program you idiot".
End Else
say "Oh I don't know i's input"
say "If the pattern conitnues.."
await user input for i.
Can you see why this is a problem? It will always output the next few lines if they are not blocked within the else.
The reason it still goes to the next question is because you don't use a while loop(to check for a condition, such as while(i != 10)
If you want to, take a look at this code I wrote for your program, see if you can figure out what certain things do on there own, if you have any questions, feel free to ask.
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
|
#include <iostream>
using namespace std;
int main()
{
int x,y,z,a,b,i;
y = 2;
z = 4;
a = 6;
b = 8;
i = 0;
int pattern = 0;
cout << "What is the following pattern?" << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "i: ?" << endl;
while (pattern != 2)
{
cout << "The pattern is: ";
cin >> pattern;
if (pattern != 2)
cout << "Wrong, try again.\n";
else
break;
}
cout << "Good job, it is 2!" << endl;
cout << "If the pattern continues, the variable i should equal: ";
cin >> i;
while (i != 10)
{
cout << "Incorrect, try again.\n";
cout << "The variable i should equal: ";
cin >> i;
}
cout << "Correct, good job!";
return 0;
}
|