#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <ctime>
usingnamespace std;
int main (void) {
srand(time(0));
int x, tryNumber;
bool correct = false;
string guess, xstring;
ostringstream convert;
cout << "Guess the number 1 - 10!\n";
while (correct != true)
{
x = rand() %10 +1;
getline(cin,guess);
convert << x;
xstring = convert.str();
if (guess == xstring)
{
correct = true;
}
else
{
cout << "That was wrong! The correct number is " << x << "\nTry again !\n";
tryNumber++;
correct = false;
}
}
cout << "Well done! You guessed the number in " << tryNumber <<" try(s)!";
cin.get();
return 0;
}
The problem is that if I enter a number that was correct, for example I am entering '1' in all the time, this would happen:
1
That was wrong! The correct number is 1
Try again !
The only time this has not happened would be when I have guessed the correct number first try, however the try number was incredibly large.
I think the conversion of the int to a string is what is causing the first problem, however I do not know how to fix it, I don't know how to fix the second problem either.
Then I wouldn't be testing the method for converting an integer into a string.
The point of the program was to test out methods I know I will find useful in the future.