OK so I'm very new to programming and have gone through a few tutorials and was hoping to create a very basic guessing game. I want it to identify the player then create a random number that the player needs to guess. after the player guesses correctly i want it to give the player the option of playing again or terminating the program. At first, i was unable to get it to allow the player to play again...but now it wont even progress through the guessing portion of the game. any suggestions as to what i am doing wrong?? here is the code. I appreciate all the help!
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
char answer;
int number;
char player[20];
int guess;
srand((unsigned)time(0));
int random = (rand()%100+1);
int y;
int n;
do
{
cout << "I want to play a guessing game, but first, please enter your name" << endl;
cin >> player;
cin.ignore();
cout << "Ok,"<<player << " lets play!\n";
cout<< "Ok, so i'm thinking of a number from 1-100...take a guess:\n";
cin>> guess;
cin.ignore();
do {
if (guess < number)
{
cout<<"Try a little bigger "<<player <<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
elseif (guess > number)
{
cout<< "Try a little smaller "<<player<<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
}
while (guess != number);
cout<<"Yay you got it right!!! Great job "<<player <<"!!\n";
cout<<"Would you like to play again (y/n)";
cin>> answer;
cin.ignore();
return answer;
} while (answer != n);
return 0;
}
ok so as soon as i posted this i figured out why it would not go through the guessing progress. i never assigned my random number to a variable, but i still cant seem to loop the game. Here is my revised code
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
char answer;
int number;
char player[20];
int guess;
srand((unsigned)time(0));
int random = (rand()%100+1);
number = random;
char y;
char n;
do
{
cout << "I want to play a guessing game, but first, please enter your name" << endl;
cin >> player;
cin.ignore();
cout << "Ok,"<<player << " lets play!\n";
cout<< "Ok, so i'm thinking of a number from 1-100...take a guess:\n";
cin>> guess;
cin.ignore();
do {
if (guess < number)
{
cout<<"Try a little bigger "<<player <<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
elseif (guess > number)
{
cout<< "Try a little smaller "<<player<<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
}
while (guess != number);
cout<<"Yay you got it right!!! Great job "<<player <<"!!\n";
cout<<"Would you like to play again (y/n)";
cin>> answer;
cin.ignore();
return answer;
} while (answer == y);
return 0;
}
i didnt check all of the code, and maybe im wrong. but if it did work, on the second time around the computer will have the same number in mind through all of the future games since the computer never picks a new number in the loop.
hmmm, that is probably right....and i did think of that but i haven't been able to test it because I can't get the game to loop at all. At the end if i put in either "y" or "n" it always terminates and i want y to cause it to loop the game again. ill probably need to put the random number sequence inside of the loop to get it to generate a random number for each game. Thank you for the input.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int answer;
int number;
char player[20];
int guess;
srand((unsigned)time(0));
int random = (rand()%100+1);
number = random;
int y;
int n;
cout << "I want to play a guessing game, but first, please enter your name" << endl;
cin >> player;
cin.ignore();
while (answer != n)
{
srand((unsigned)time(0));
int random = (rand()%100+1);
cout << "Ok,"<<player << " lets play!\n";
cout<< "Ok, so i'm thinking of a number from 1-100...take a guess:\n";
cin>> guess;
cin.ignore();
do {
if (guess < number)
{
cout<<"Try a little bigger "<<player <<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
elseif (guess > number)
{
cout<< "Try a little smaller "<<player<<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
}
while (guess != number);
cout<<"Yay you got it right!!! Great job "<<player <<"!!\n";
cout<<"Would you like to play again (y/n)";
cin>> answer;
cin.ignore();
return answer;
};
return 0;
}
it is probably something simple that i am missing but i am completely stumped on this.
#include <iostream>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
void Start ();
void GetResults ();
int i, j, life, maxrand;
char c;
void
Start ()
{
i = 0;
j = 0;
life = 0;
maxrand = 6;
cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
cout << "1 : Easy (0-15)\n";
cout << "2 : Medium (0-30)\n";
cout << "3 : Difficult (0-50)\n";
cout << "or type another key to quit\n";
c = 30;
cin >> c; // read the user's choice
cout << "\n";
switch (c)
{
case'1' : maxrand = 15; // the random number will be between 0 and maxrand
break;
case'2' : maxrand = 30;
break;
case'3' : maxrand = 50;
break;
default : exit(0);
break;
}
life = 5; // number of lifes of the player
srand( (unsigned)time( NULL ) ); // init Rand() function
j = rand() % maxrand; // j get a random value between 0 and maxrand
GetResults();
}
void
GetResults ()
{
if (life <= 0)
// if player has no more life then he lose
{
cout << "You lose !\n\n";
Start();
}
cout << "Type a number: \n";
cin >> i; // read user's number
if ((i>maxrand) || (i<0)) // if the user number isn't correct, restart
{
cout << "Error : Number not between 0 and \n" << maxrand;
GetResults();
}
if (i == j)
{
cout << "YOU WIN !\n\n"; // the user found the secret number
Start();
}
elseif (i>j)
{
cout << "Too BIG\n";
life = life - 1; // -1 to the user's "life"
cout << "Number of remaining life: " << life << "\n\n";
GetResults();
}
elseif (i<j)
{
cout << "Too SMALL\n";
life = life - 1;
cout << "Number of remaining life:\n" << life << "\n\n";
GetResults();
}
}
int
main ()
{
cout << "** Jackpot game **\n";
cout <<" "<<endl;
cout << "The goal of this game is to guess a number. You will be ask to type\n";
cout << "a number (you have 5 guess)\n";
cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to find\n\n";
Start();
return 0;
}
it works, and i understand why it works...and it's definitely a better way of writing it than mine but i want to understand why mine doesn't work. I can't seem to find the error....
thats a nice one alex. even has difficulty settings =D. your code sokol has several revisions and i sope i am referrring the right one but you check against an uninitalized int n. answer is the wrong type and never used.
so if i make answer a char and drop out int y and int n should that fix it? also i thought that a while loop checked the condition each time it went through the loop?
fixed i think. assigning random to number is kinda pointless you can just test against random but i didnt want to fix it all. it works as is now. also moved was the random seed outside the loop it should only be initialized once.
#include <iostream>
#include <string>
#include <ostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
usingnamespace std;
int main()
{
char answer = 'y';
int number;
char player[20];
int guess;
int random;
cout << "I want to play a guessing game, but first, please enter your name" << endl;
cin >> player;
cin.ignore();
srand((unsigned)time(0));
while (answer == 'y')
{
random = (rand()%100+1);
cout << "Ok,"<<player << " lets play!\n";
cout<< "Ok, so i'm thinking of a number from 1-100...take a guess:\n";
cin>> guess;
number = random;
cin.ignore();
do {
if (guess < number)
{
cout<<"Try a little bigger "<<player <<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
elseif (guess > number)
{
cout<< "Try a little smaller "<<player<<"!\n";
cout<< "please enter a number:\n";
cin >> guess;
cin.ignore();
}
}
while (guess != number);
cout<<"Yay you got it right!!! Great job "<<player <<"!!\n";
cout<<"Would you like to play again (y/n)";
cin>> answer;
};
return 0;
}
yeah the random seed being in there twice was me moving some stuff around playing with it. forgot to take it out. So it appears that i needed 'y' instead of just y??? not sure why but maybe i just haven't hit that part of the tutorial yet. I appreciate all the help, thank you!