Hello forum.I am truly sorry this is my first post,but as I am a terrible programmer who has just begun(or might I say,just 'properly' started) C++.I am have a problem with my 'guess my number'game where a 'computer' has to guess the numbers.But I seem to have a problem on my application.
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
|
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
char answer;
int tries = 0;
do {
cout << "choose a number between 1 and 100 and I'll guess it." << endl;
cout << "is " << number_guess << " your number?(y/n)" << endl;
++tries; // calculates how many tries it took the computer
cin >> answer;
if (answer == 'y')
cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" << endl;
} while (answer == 'n'); // continue until the computer is wrong.
system("pause");
return 0;
}
| |
Now,this is designed so that I secretly pick the number between 1 and 100,then computer has to guess it.I tell the computer if he is wrong or right(y/n) then computer either continues guessing,or he congratulates himself.
Here is the problem.
The computer,is guessing the EXACT same number each time.So what I mean by that is,actually,I'll give you an example.
lets say my computer guess 12 and he is wrong.
Next turn,he guessess 12 AGAIN.Or should I say,
HE GUESSESS THE SAME NUMBER OVER AND OVER AND OVER.
This seem to be my fault,and because I am a beginner,I can't seem to find a problem.Please help forum users.Thank you in advance.