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.
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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.
So within that do-while loop, the computer presents the variable number_guess as its guess. I see where that value gets set, at the start; tell me, where do you ever change that value so that the computer doesn't always guess the same number?
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
number_guess = rand() % 100 + 1;
} while (answer == 'n'); // continue until the computer is wrong.
system("pause");
return 0;
}
I suppose I moved that number_guess to the right place.
Well to explain you things, firstly you declare a variable number_guess and give him random value, later you don't change that value, but simply take the variable and check it again for equivalence. So you need after each incorrect guess add new value to variable number_guess, so just adding number_guess = rand() % 100 + 1; at the end of do...while loop is what you needed.
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace std;
int main()
{
srand(time(0));
int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100. <-- this code needs to go in the do-while loop.
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. <-- Do you mean until the computer is right?
system("pause");
return 0;
}
This is going to take a long time for the computer to guess the number, as it is just guessing totally randomly. Perhaps you should store which numbers have already been guessed in an array?
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace std;
int main()
{
int tries = 0;
char answer;
do {
srand(time(0));
int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
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 back!" << endl;
number_guess = rand() % 100 + 1;
} while (answer == 'n');
// continue until the computer is wrong.
system("pause");
return 0;
}
I took Daniels' advice and moved the guess_number outside,and I also moved the int tries outside as well,because it was generating a problem that nomatter how much tries it actually did,the computer always claimed that it did it in 1 shot.Now the problem is solved,but,,,,
This is going to take a long time for the computer to guess the number, as it is just guessing totally randomly. Perhaps you should store which numbers have already been guessed in an array?
That is a good suggestion.I would do that but I'm just not that advanced :D
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace std;
int main()
{
int tries = 0;
srand(time(0));
int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
int counter = 0;
int array[100];
bool errorfound = true;
char answer;
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 back!" << endl;
else
{
array[counter] = number_guess;
counter++;
}
number_guess = rand() % 100 + 1;
while(errorfound == true)
{
int e = 0;
for(int i=0; i<100; i++)
{
if(number_guess == array[i])
{
number_guess = rand() % 100 + 1;
e = 1;
break;
}
}
if(e != 1)
errorfound = false;
}
} while (answer == 'n');
// continue until the computer is wrong.
system("pause");
return 0;
}
I suppose it might be something like that. Not compiled it and not tried, maybe with errors, but the main point is here.
Seems like using a sledge to drive in a wood nail; it's just too much.
1) Don't repeat code. If you find yourself typing exactly the same command twice, you're doing it wrong.
2) srand(time(0)); should not be used more than once per program. It's a seed generator, not a random number generator.
3) Indentation rocks. It makes people not go cross-eyed when they try to make sense of where your nests begin and end.
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace std;
int main()
{
int number_guess = 0; //Good to set variables to zero.
bool array[100];
shortint i = 0; //Just to go around this next while loop.
while(i < 100)
{
array[i] = 0; //Set all the array to 0.
++i;
}
srand(time(0));
char answer;
int tries = 0;
cout << "choose a number between 1 and 100 and I'll guess it." << endl; //You only want to say this once, so it does not need to be in the loop.
do
{
number_guess = rand() % 100 + 1;
if(array[number_guess] == 1) //If this number in the array is true (or set to one in other words).
{
//Do nothing and we go around the loop again, generating a different, random number.
}
else //Otherwise.
{
array[number_guess] = 1; //Mark off that position in the array as taken.
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. <-- Do you mean until the computer is right?
system("pause");
return 0;
}