int main()
{
int random_number;
int guess;
srand(time(0));
random_number = (rand()%10) +1;
string userName;
cout << "Please enter your username: " << endl;
cin >> userName;
do
{
cout << "Hello " << userName << endl;
cout << "Try to guess what number I have stored in my memory?" << endl;
cout << "Here is hint. Its between 1 to 10" << endl;
cin >> guess;
if(guess == random_number)
{
cout << "You guessed right!!!" << endl;
}
else if(guess < random_number)
{
cout << "The number is higher!" << endl;
}
else if(guess > random_number)
{
cout << "The number is lower!" << endl;
}
}
while(random_number != guess);
cout << "Nice work " << userName << endl;
}
The game to play is a well known number guessing game. Alice asks Bob to pick a secret number between 1 and 15. When Bob has selected his number, Alice will guess it. If Alice's guess is correct Bob will respond �You win�. If his secret number is lower than Alice's guess, Bob will reply �lower�, and Alice will guess again. If his number if higher, he will respond �higher�, and Alice will guess again. If the number is correct, Bob will reply �You win�, Alice wins, and the game is over. If Alice does not find the correct number within 10 attempts, she gives up, and Bob wins.
The task is to write a program that plays for Alice. This means, your program will repeatedly guess Bob's secret number, print the guess to standard output, reads Bob's answer from standard input, and give up after 10 failed attempts
char relation(){
char c;
std::cout << " Is the number High(H),Lower(L) or Correct(C)?:";
std::cin >> c;
return tolower(c);
}
//gets the letter function
int limit = 15;
bool guessed = false;
while(!guessed){
srand(counter++);
if(counter == 10) {std::cout << "i give up!"; return 0;}
std::cout << "is your number " << rand()%limit+1;
switch(relation()){
case 'h':
case 'l':
case 'c':
default:
std::cout << "invalid letter entered";
}
//basic outline for your main function.
int getnumber(){
int i = -1;
while(i > 15 && i < 0){
std::cout << "enter in your number: ";
std::cin >> i;
if(!(i > 15 && i < 0)) std::cout << "you cheated!";
}
return i;
}
use code tags and do not ask people to fill out the code i helped you by giving you... do your own work.
if you are reading this please do not fill out the code kanaosa posted. i gave him that code and he has to do his own work for his own grade.
Isn't the srand() function seeded wrong here.
I'm not entirely sure how it works but in a book i have it shows this and it shows that when seeding the random number with the time you should use casting.
example similar to what's shown in "Beginning C++ Through Game Programming Third Edition"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstdlib>
#include <time>
int main(){
srand(static_cast<int>(time(0)));
while(true){
int random = (( srand() % 6 ) +1);
cout << random << endl;
system("pause");
}
}
//This would be for a single dice simulation program for example
If it is possible to simply use "srand(time(0));" then i've been wasting a lot of time!
Is This Possible???