Begginer practice problems

I am going through the beginner practice problems on this C++ thread.

http://www.cplusplus.com/forum/articles/12974/

I am stuck on this particular part of the program called while (user == gullible). Here is the code I have written out so far. Nothing too complicated.

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
// Gullible     Patric Bernard     

#include <iostream>
using std::cin; /* Using std::cin,cout, and endl are just so I don't have to include all of using namespace std;*/
using std::cout;
using std::endl;                   


int main()
{
    int x = 5; // x is the variable that the user has to enter to end the program.
    int y,z; // y is the variable that the user enters, z is used for a counter in my for loop.
    
    cout << "Please enter a number that isn't " << x <<"!" << endl;
    cin >> y;
    
    for(z = 0; z <= 9; z++) //for loop repeats 10 times.
    {
          x = x + y;
      if(z > 8) //if the for loop repeats 10 times, the program will run this iteration, terminating the program.
        {
        cout << "Wow, you have a lot more patience than I do. You win!" << endl;
        cin >> y; // only used for a holding point in the program so the user can see the end result. Entering a varialbe will end the program.
        }
      else if (x == y) //if the user enters the number they are asked not too, this else if loop will scold them, and then break out of the for loop, ending the program.
        {
        cout << "You entered the number " << y << "!" << endl;
        cout <<"Hey! You weren't supposed to enter the number " << x << "!"<< endl;
        cin >> y;
        break;
        }
      else if (x != y) // if the user does not enter the value they are told not too, they will keep being asked to enter in a value.
        {
        cout << "You entered the number " << y << "!" << endl;
        cout << "Please enter a number that isn't " << x <<"!" << endl;
        cin >> y;
        }
    }


The issue I am having is with getting the numbers to add up on the final challenge of this program. If I enter in x = x + y; right after I open my for loop, I can get the values to start adding up, and saying please do not enter the value of x, which is the sum of x and y (y being the users entered value) but when I enter in the value they are not supposed to enter, It does not terminate the program.

Can anyone give me some help on this?
Line 19: x = x + y;
If y was equal to x, now x is equal to 2*y.
That means you should compare x with 2*y, and not with y.
Awesome. That's it. Can't believe I overlooked that hah.

Here's the completed program.

Any hints or ideas on improving the design or layout of it?

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
40
41
// Gullible     Patric Bernard     

#include <iostream>
using std::cin; /* Using std::cin,cout, and endl are just so I don't have to include all of using namespace std;*/
using std::cout;
using std::endl;                   


int main()
{
    int x = 5; // x is the variable that the user has to enter to end the program.
    int y,z; // y is the variable that the user enters, z is used for a counter in my for loop.
    
    cout << "Please enter a number that isn't " << x <<"!" << endl;
    cin >> y;
    
    for(z = 0; z <= 9; z++) //for loop repeats 10 times.
    {
          x = x + y;
      if(z > 8) //if the for loop repeats 10 times, the program will run this iteration, terminating the program.
        {
        cout << "Wow, you have a lot more patience than I do. You win!" << endl;
        cin >> y; // only used for a holding point in the program so the user can see the end result. Entering a varialbe will end the program.
        }
      else if (x == 2*y) //if the user enters the number they are asked not too, this else if loop will scold them, and then break out of the for loop, ending the program.
        {
        cout << "You entered the number " << y << "!" << endl;
        cout <<"Hey! You weren't supposed to enter the number " << x-y << "!"<< endl;
        cin >> y;
        break;
        }
      else if (x != 2*y) // if the user does not enter the value they are told not too, they will keep being asked to enter in a value.
        {
        cout << "You entered the number " << y << "!" << endl;
        cout << "Please enter a number that isn't " << x <<"!" << endl;
        cin >> y;
        }
    }
    
    return 0;
}
Topic archived. No new replies allowed.