Just need some feedback.

Hello World!

I recently (2 months ago) decided to learn programming and started with C++ and i would like to get some feedback on this little number guessing game i wrote. Are there any newbie mistakes in it that should be corrected or is there a better/more efficient way of writing this? (i mean things like this: before finding this forum the last line was system ("PAUSE"); :)

What the code does: generates a random number between 0 and 100 and the user has to guess it, i experimented with writing it "fool proof" so the program wont go mad if you imput apple instead of a number. If you find a way to crash it or make it go mad plz let me know (only what you did to get an error, not the solution THX)

How can i write it so that it wont wait for pressing enter after the 0 or 1 when it asks if you want to play again?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    srand(time(NULL));
    bool play_again=true;
    do
    {
    int user_error1=0;
    int user_error2=0;
    int player_guess;
    int secret_number=rand()%101;
    cout<<"Guess the number! It's between 0 and 100"<<endl;
    int i=0;
    //cout<<secret_number; //cheat line :)
    do
    {
        i++;
        cout<<"Whats your "<<i<<". guess?"<<endl;
        do
        {
            if (!(cin >> player_guess))
            {
                user_error1=1;
                cout<<"Please enter numbers only!"<<endl;
                cin.clear();
                cin.ignore(10000,'\n');
            }
            else {user_error1=0;}
            if ((player_guess<0)||(player_guess>100))
            {
                user_error2=1;
                cout<<"Remember the number is between 0 and 100"<<endl;
            }
            else {user_error2=0;}
        } while ((user_error1==1)||(user_error2==1));
        if (player_guess>secret_number) {cout<<"Thats too high!"<<endl;}
        if (player_guess<secret_number) {cout<<"Thats too low!"<<endl;}
    } while (player_guess!=secret_number);
    if (i==1)
    {
        cout<<"Lucky guess! :)"<<'\n';
    }
    else {cout<<"Congratulations you guessed the secret number in "<<i<<" guesses!\n";}
    cout<<"Do you wish to play again? Input 1 for yes 0 for no";
    do
    {
        if (!(cin>>play_again))
        {
            user_error1=1;
            cout<<"Please enter 1 if you wish to play again 0 if you don't!"<<endl;
            cin.clear();
            cin.ignore(10000,'\n');
        }
        else {user_error1=0;}
    } while (user_error1==1);
    } while (play_again==true);
    cout<<"Thx for playing! Bye! Press enter to exit.";
    cin.ignore(10000,'\n');
    cin.get();
    return 0;
}
Looks good.

Let's puzzle you.
1. What will happen when user enters "5Good" and random number is "5".
2. What will happen when user enters "7Number" and random number is "70"
3. What will happen when user enters "Number8" and random number is "8"

How can i write it so that it wont wait for pressing enter after the 0 or 1 when it asks if you want to play again?


I don;t think there is any standard C++ function to do this, you can use plain C function
1
2
#include <conio.h>
play_again = getch();
A nice next step would possibly be moving the code into a class, so your main might look something like this:

1
2
3
4
5
6
7
int main()
{
    Game myGame;
    myGame.Run();

    return 0;
}
thx you for your feedback

Santosh

i didnt test it with a mix of letters and numbers...

what puzzles me is line 26
correct me if im wrong: this line should check if the variable player_guess is an integer
if (!(cin >> player_guess))
but when it gets "67asd" as input it still considers player_guess to be in integer format
what am i missing here?

mutexe

i just finished reading the Compound Data Types chapter, so atm i'm not familiar with classes. (still trying to figure out pointers) I saw it a lot that people try to write their program the way you suggest. Why is this a better solution then writing the code inside the main function?
Last edited on
if (!(cin >> player_guess))
This will return the istream (cin) itself, which can never be 0, so the if is always true.

The correct way would be to
1
2
3
4
5
    cin >> player_guess;
    if(player_guess > 0)
    {
        ...
    }


but when it gets "67asd" as input it still considers player_guess to be in integer format

this is the beauty of the >> operator
@Santosh cin can be true or false, depending on what happened during the input attempt.
I'm trying to write some code that will only accept numbers and numbers only as input, i believe i got this part right but i encountered a strange error when converting the string of characters to an integer.
I check every character entered and if one has a vale thats not in the 48-57 range (these are the character values of 0-9 in the ASCII table) it will throw an error message and wait for another input.
The bug is when i convert the string of characters to an integer (line 28) i will get the following outputs for x. If the character string is 3 chars long it gives back a wrong value otherwise it functions as intended. It probably has to do something with the pow function im using but cant figure out what the problem is


in out
7 7
75 75
756 755
7564 7564


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
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    string input="";
    bool user_error=0;
    int x=0;
    do
    {
    x=0;
    user_error=0;
    getline(cin,input);
    for (int i=0;i<input.size();i++)
      {
        char c;
        c=input[i];
        if ((c<48)||(c>57))
        {
            cout<<"Please enter numbers only!\n";
            user_error=1;
            break;
        }
        else {int p=input.size()-i-1;x+=(c-48)*pow(10.0,p);}
        //cout<<pow(10,input.size()-i-1)<<'\n';
      }
    } while (user_error==1);
    cout<<x;
    return 0;
}


thx for the links mutexe
Last edited on
Topic archived. No new replies allowed.