[try Beta version]
Not logged in

 
 
Computer guess my number

Dec 23, 2012 at 5:35am
I know this has popped in other areas on this site but please bare with me. I just started programming in c++ earlier this week and I am working my way through a book.

There was practice programming challenge in the book. It asked to write a program that had the computer try to guess your number. I came up with this and wanted to know if this seemed like an okay way to approach this type of program.

Any assitance will be very much awesome!

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(time(0)); //generates the random numbers

//variables

int compnumber = rand() % 5 + 1;
int mynumber;
int newnumber;

cout << "Welcome to the number game, I'll try to guess your number!\n\n";
cin >> mynumber;






while (compnumber > mynumber)

{
cout << "I guessed too high\n\n";
cout << "\nI had guessed the number " << compnumber << "\n\n";
compnumber = rand() % 5 + 1;
cout << "Enter your number again please\n\n";
cin >> mynumber;
}


while (compnumber < mynumber)
{
cout <<"\nI guessed too low\n\n";
cout << "I had guessed the number " << compnumber << "\n\n";
compnumber = rand() % 5 + 1;
cout << "Enter your number again please\n\n";
cin >> mynumber;
}


if (compnumber == mynumber)
{
cout << "\nThat was easy, human!\n\n";
cout << "I guessed " << compnumber << "\n\n";
}



system ("pause");

return 0;

}
Dec 23, 2012 at 6:15am
Yes, it is a very good approach, but this part can be omitted and u can still get the result.
1
2
cout << "Enter your number again please\n\n";
cin >> mynumber;

Another thing, instead of using system("pause"); try using cin.ignore();
And the last thing is the following, in order to delete the first thing i mentioned u need to change the while buckles for if conditions and write a do while buckle this way,
1
2
3
do{
//guessing stuff
}while (compnumber !=mynumber)

However, those are simple recommendations or preferences I have. Well done =)
Dec 23, 2012 at 6:48am
Thank you so much for your input in such short notice! Very helpful. I usually just run the batch file instead of using pause. But I will try what you recommend!

Also. If you don't mind. I was a little confused about some very newbish stuff covered in my c++ book. It was about the whole iostream and standard namespace. I understand that cin cout are objects of the standard namespace. I also understand that iostream is also needed for these objects but I just don't understand the difference between the two? :/
Dec 23, 2012 at 11:28am
Can you please always use the code tags - the <> button on the right.


It's not they are different, cin etc are part of the std namespace.

A namespace is a way separating out variable and function names to avoid name clashes which is more likely to happen if there is just one global namespace.

Basically, iosteam is a class that has cin etc , plus all kinds of other things like the operators in it. iostream stands for input output stream. One needs to #include <iostream> to have access to the functionality the class offers.

Instead of using namespace std; you can do a couple of things:

1. put std:: before each std thing, as in std::find

2. put these after the include directives (for frequently used things):

1
2
3
4
using std::cin;
using std::cout;
using std::endl;
using std::string;


then you can refer to cout etc without any further qualification.

I tend to do a mixture.

If you look in the reference section at the top left of this page, you will see that all things in the STL need to use either of these methods.

Unfortunately the example code has using namespace std; throughout, which is a little bad IMO.

Anyway, try your hand at Google for these topics, Hope this all helps. Cheers.



Last edited on Dec 23, 2012 at 11:32am
Dec 23, 2012 at 2:18pm
Thanks again for the help! I guess I was confused because it did not make sense that if iostream defined cin and so on that I would also need to define which namespace to use. More or less I'm assuming it means that cin, cout, etc belong to standard namespace and are defined in iostream.

Thanks again.
Dec 25, 2012 at 3:43am
only if you care to check out

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
/* Another version of the computer guess my number game.
This version as you can see works a little bit different than my other version.
Let me know what you think and any improvments that could be made! */

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
    srand(time(0)); //this seeds "random" numbers based on time 
    
    int computer_guess = rand() % 10 + 1; // calculates random numbers between 1 and ten
    char yes = 'y';  //the response the user makes for the correct answer 
    char no = 'n';    //response use makes for the wrong answer
    int tries = 1;
    char user_answer;
    int calc = rand();
    
    cout << "Are you thinking of the number " << computer_guess << " (y/n).\n";
        cin >> user_answer;
        
    
    while (user_answer == 'n')
    
    {
        
            
            
               tries++; //attempt tracker
               cout << "\nLet me try again!\n";
               cout << "\nIs it the number " << rand() % 10 + 1 << "?\n";  //calcs again
               cin >> user_answer; //user response
               
               
                   

     } 
     
    
    cout << "\nI knew it all along! It only took me " << tries << " attempt(s)!\n"; //correct response
    
    system ("pause");
    return 0;
}
Dec 25, 2012 at 4:25am
I think the way you did it before will be faster than this. I don't really remember much about rand but from what I see, you are not using any variables or conditions in the rand which means that the computer will be getting quite random values. So it will take longer to complete the guessing game.

Use a MAX and MIN varaible to keep track of the guesses made and adjust guess depending on user response.

ex. Have the computer ask the user if it guessed too high or too low and adjust the range of guesses by the answer recieved. Will be a much faster and more interesting gameplay that way.


An advanced method for this will be using trees.
Last edited on Dec 25, 2012 at 4:27am
Dec 27, 2012 at 8:07pm
Thanks alot!! Alot of the things you mentioned I have not come across yet. I am working out of a book right now. I just started programming last week so bare with me on any sillyness I do.

I was considering rewriting the code like you said with min and max and adjusting it so each new guess was actually narrowing in the actual number instaed of truly plain random numbers. Next I learn arrays and this may help with this. I thnk u can store values in an array and tell the computer not to call those values again.
Topic archived. No new replies allowed.