Help with Guess my Number!

Here's the deal, I'm trying to create a guess my number game where the computer tries to guess the Number that the user inputs. But I want to make it so that the user only needs to input his/her number once. Now here's the problem: If I put it in the do while loop, it keeps printing it out on the screen. (Yes I know that it should do that) But if I put it OUTSIDE the do while loop; I input a number and it creates an infinite loop. God knows why.
Here's the 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 <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));

    int UserNum;
    int tries = 0;
    int CompGuess;


    cout << "\t     Welcome to Guess My Number\n";
    cout << "\n\nIn this version of the game the Computer tries to guess YOUR number.";

    
    
    do
    {

       cout << "\n\nPlease Enter the Number You Want Me to Guess: ";
       cin >> UserNum;
       ++tries;



        if(CompGuess > UserNum)
        {
            cout << "Too High!";
        }

        else if(CompGuess < UserNum)
        {
            cout << "Too low!";
        }

        else
        {
            cout << "That's it, you got it in " << tries << " guesses.";
        }


    }while(CompGuess !=UserNum);
}


I'm sure that there is a million more problems with this code, and if there is, could you kindly point them out for me to correct.
Apologies if they are any stupid mistakes in the code, I tend to make stupid mistakes.
FYI I'm a beginner if you didn't know.
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 <cstdlib>
#include <ctime>

int main()
{
    std::srand(static_cast<unsigned int>(std::time(0)));

    int UserNum;
    int tries = 0;
    int CompGuess;

    std::cout << "\t     Welcome to Guess My Number\n" << std::endl;
    std::cout << "In this version of the game the Computer tries to guess YOUR number." << std::endl;

    std::cout << "Please Enter the Number You Want Me to Guess: " << std::endl;
    std::cin >> UserNum;

    do
    {
         ++tries;
         CompGuess = std::rand() % 201; //0 to 200, including 200.

        if (CompGuess > UserNum)
        {
            std::cout << "Too High![" << CompGuess << "]" << std::endl;
        }
        else if (CompGuess < UserNum)
        {
            std::cout << "Too low![" << CompGuess << "]" << std::endl;
        }
        else
        {
            std::cout << "That's it, you got it in " << tries << " guesses.[" << CompGuess << "]" << std::endl;
        }

    }while(CompGuess != UserNum);
}


now you may want to make it smarter, and do checks to make sure the user doesn't enter under 0 or more than 200
Last edited on
Thanks, now I can see how to do it and how it works :D
Last edited on
Btw, was it a good start for a beginner? I've only been doing C++ for about 3-4 weeks
Yeah, I made the same thing a few weeks after I started, but I did it the other way around, the user had to guess the number, I think it makes it more fun since its the user that plays, whereas with yours they enter a number for the whole of the 'game'
Last edited on
Topic archived. No new replies allowed.