Need help with while excercise

Hello guys!

I'm currently doing this excercise:
While( user == gullible )
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)

Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.

★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.

★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)

But i'm stuck at the '10 iterations part'..
This is where i'm at:

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
#include <iostream>

using namespace std;

int main()
{

    int number;
    int counter = 0;
    
    
    
    while (number != 5)
    {
    cout << "Enter a number other than 5: ";
    cin >> number;
    counter++;

    while (counter==10)
    {
        cout << "Wow, you're more patient then I am, you win." << endl;
        cin.get();
    }

    }

    cout << "Hey! you weren't supposed to enter 5!";




    return 0;
}
Last edited on
1
2
3
4
5
    while (counter==10)
    {
        cout << "Wow, you're more patient then I am, you win." << endl;
        cin.get();
    }


Should be :
1
2
3
4
5
if(counter==10)
{
        cout << "Wow, you're more patient then I am, you win." << endl;
        cin.get(); break;
}
Ah thanks man!

Now the correct code looks like this:

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 number;
    int counter = 0;


    while (number != 5)
    {
        cout << "Enter a number other than 5: ";
        cin >> number;
        counter++;

    if(counter == 10)
    {
        cout << "Wow, you're more patient then I am, you win." << endl;
        break;
    }

    if(number == 5)
    {
        cout << "Hey! you weren't supposed to enter 5!";
        break;
    }

    }






    return 0;
}
Last edited on
1
2
3
4
5
if(counter==5)
{
        cout << "Hey! you weren't supposed to enter 5!";
        break;
}


Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate.
Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

http://en.cppreference.com/w/cpp/language/break
In this scenario, adding break does nothing.

You don't need if(number == 5). If the user enters 5, while (number != 5) evaluates to false, thus terminating the loop. Also, prefer "\n" over std::endl.
1
2
3
4
5
6
7
8
9
10
11
12
13
while ( number != 5 )
{
    cout << "Enter a number other than 5: ";
    cin >> number;
    counter++;

    if( counter == 10 ) {
        cout << "Wow, you're more patient then I am, you win.\n";
        break;
    }
}

cout << "Hey! you weren't supposed to enter 5!";



edit: slight mistake, thanks SakurasouBusters.
edit2: Disregard this comment. Today isn't my day :/
Last edited on
1
2
if( counter == 10 )
cout << "Wow, you're more patient then I am, you win.\n";


So after the user enters 10 numbers and the program displays "Wow, you're more patient then I am, you win.", it still prompts the user to enter more numbers???
SakurasouBusters wrote:
So after the user enters 10 numbers and the program displays "Wow, you're more patient then I am, you win.", it still prompts the user to enter more numbers???

Woops, my bad.
Last edited on
Topic archived. No new replies allowed.