While ( user == gullible )

Hello, I am fairly new to programming and found a set of beginner-level problems in this forum ( http://www.cplusplus.com/forum/articles/12974/ ).

I have a question about this problem:

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.)

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

using namespace std;

int main()
{
    int number;
    int j = 0;
    string character;
    cout << "Please enter any number other than 5: ";
    while (true)
    {
        getline(cin,character);
        stringstream temp(character);
        if (temp >> number)
        {
            while (number != 5)
            {
                while (j < 9)
                {
                    cin >> number;
                    j++;
                }
                cout << "I give up.";
                return 0;
            }
            cout << "You win!";
            break;
        }
        else
        {
            cout << "You did not enter a number. Please try again: ";
        }
    }
    return 0;
}


__________________________________________________________

Now, the problem when I run the program is this:

Please enter any number other than 5:
1
2
3
a
I give up.
Process returned 0 (0x0) execution time : 2.969 s
Press any key to continue.
__________________________________________________________

After I input a number, everything works fine until I input a character.
Any help on this one?
Last edited on
So I tweaked things a little bit and solved the problem:

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
42
43
44
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    int number;
    int j = 0;
    string character;
    cout << "Please enter any number other than 5: ";
    while (true)
    {
        getline(cin,character);
        stringstream temp(character);
        if (temp >> number)
        {
            while (number != 5)
            {
                while (j < 9)
                {
                    getline(cin,character);
                    stringstream temp(character);
                    if (temp >> number)
                    {
                            j++;
                    }
                    else cout << "You did not enter a number. Please try again: ";

                }
                cout << "I give up.";
                return 0;
            }
            cout << "You win!";
            break;
        }
        else
        {
            cout << "You did not enter a number. Please try again: ";
        }
    }
    return 0;
}


Any tips on making it shorter? Or say, more efficient?

Thanks!
Last edited on
Good job with using stringstreams to validate input!
Your program logic is not exactly correct, if I enter 1 and then 5, it doesn't tell me I incorrectly entered 5. Also, if I entered 5 as the first number it says I win...

Also, you have repeated code logic and 3x embedded while loops, which will certainly lead to confusion.

Here's how I would write your program:
Foremost, I would put number input and validation its own separate function so that the logic of number validation is separate from the actual logic of the code. I called this function "get_number".

Here's my version of your code:
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
42
43
44
45
46
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int get_number()
{
    int number;
    std::string input;
    while (true)
    {
        cout << "Please enter any number other than 5: ";
        cin >> input;
        stringstream ss(input);
        
        if (ss >> number)
            break;
        else
            cout << "You did not enter a number. Please try again: ";
    }
    return number;
}

int main()
{
    int iterations = 0;
    while (true)
    {
        int number = get_number();
        if (number != 5)
        {
            iterations++;
            if (iterations == 10)
            {
                cout << "Wow, you're more patient then I am, you win." << endl;
                break;
            }
        }
        else
        {
            cout << "Hey! you weren't supposed to enter 5!" << endl;
            break;
        }
    }
}
Last edited on
Oh yeah I didn't realize that.
I worked on that after midnight, so I was quite tired.
This one should be correct:
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
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    int number;
    int j = 0;
    string character;
    cout << "Please enter any number other than 5: ";
    while (true)
    {
        getline(cin,character);
        stringstream temp(character);
        if (temp >> number)
        {
            while (number != 5)
            {
                while (j < 9)
                {
                    getline(cin,character);
                    stringstream temp(character);
                    if (temp >> number)
                    {
                        if (number != 5)
                        {
                            j++;
                        }
                        else {cout << "You win!";}
                    }
                    else cout << "You did not enter a number. Please try again: ";

                }
                cout << "I give up.";
                return 0;
            }
            cout << "You win!";
            break;
        }
        else
        {
            cout << "You did not enter a number. Please try again: ";
        }
    }
    return 0;
}
Last edited on
@Ganado

Thank you for your reply!

I edited my code just now.

I finally understood how stringstream works now that I've read your code.

I didn't really understand how stringstream validation works because I just 'know' it from looking at other people's code.

I just started programming a few days ago(maybe less than a week) and haven't learned functions yet. I just learned up to arrays.

But it seems clear to me that function is not that hard after looking at your code.

Do you have any tips for a beginner to learn C++ effectively?
Last edited on
Topic archived. No new replies allowed.