#include <iostream> #include <string> using namespace std; int main() { int x, y; y = rand() % 10; x = 0; cout << "Hello!" << endl; cout << "What is your name?" << endl; string myName; cin >> myName; cout << "Well, " << myName << ", I'm thinking of a number between 1 and 10." << endl; cout << "Take a guess!" << endl; cin >> x; while (x != y) { if (x < y) { cout << "Your guess is too low." << endl; cout << "Take a guess!" << endl; cin >> x; } else if (x > y) { cout << "Your guess is too high." << endl; cout << "Take a guess!" << endl; cin >> x; } else if (x == y) cout << "Congratulations." << endl; } system("pause"); return 0; } |
srand ( time ( 0 ) );
at the beginning of the program.rand() % 10
wil give you a number from 0 to 9.rand() % 11
will return 0-10 and rand() % 10 + 1
will give 1-10. Also, when you ask a number 'between 1 and 10' the user will probably understand 2-9.