I've been working on a project for 3 hours now and I've got this random guessing game to work flawlessly but i'm having a hard time creating an array that can keep track of the number of guesses a person takes before getting the random number right. I am completely lost. Here is my code so far.
#include <iostream>
#include <stdint.h>
#include <time.h>
#include <fstream>
usingnamespace std;
char choice;
int isecret;
void describe_program()
{
cout << "The object of this game is to guess the random number that's in-between 1 and 100 in as few attempts as possible. ";
cout << "If you guess the number right you win the game, if not you can try until you get it right. ";
}
void PlayGame()
{
int guess;
switch (choice)
{
case'Y':
case'y':
{
cout << isecret << endl;
cout << "Enter a guess between 1 and 100\n";
cin >> guess;
do
{
if (guess < isecret)
{
cout << "Your guess is too low\n";
cout << "Try and guess again\n";
cin >> guess;
}
elseif (guess>isecret)
{
cout << "Your guess is too high" << endl;
cout << "Try and guess again" << endl;
cin >> guess;
}
if (guess == isecret)
{
cout << "Congradulations, you guess the number right\n";
}
} while (guess != isecret);
break;
}
case'N':
case'n':
{
return;
}
}
}
int main()
{
describe_program();
cout << "If you want to play the game enter 'Y' if not enter 'N' to exist." << endl;
cin >> choice;
srand(time(0));
isecret = rand() % 100 + 1;
while (choice != 'N' && choice != 'n')
{
isecret = rand() % 100 + 1;
PlayGame();
cout << "Do you want to play the game again" << endl;
cin >> choice;
}
return(0);
}
Wish my teacher made it that simple, but i have to use the array because i have to keep track of the users average for each game played and then finally display it when they decided to exist the game, i have to show them the number of guesses for each individual game at the end of the program, not after each game they finish. hope that makes sense.
void describe_program()
{
cout << "The object of this game is to guess the random number that's in-between 1 and 100 in as few attempts as possible. ";
cout << "If you guess the number right you win the game, if not you can try until you get it right. ";
}
void PlayGame(int* nog, int i, int isecret) // nog is the array.
{
// Im not sure why you had a switch statement in here.
int guess;
cout << isecret << endl;
cout << "Enter a guess between 1 and 100\n";
do
{
cin >> guess;
if (guess < isecret)
{
cout << "Your guess is too low\n";
cout << "Try and guess again\n";
nog[i] = nog[i] + 1; // increase amount of guess each guess.
}
elseif (guess > isecret)
{
cout << "Your guess is too high" << endl;
cout << "Try and guess again" << endl;
nog[i] = nog[i] + 1; // increase amount of guess each guess.
}
else
{
cout << "Congradulations, you guess the number right\n";
}
} while (guess != isecret);
}
int main()
{
char choice; // Dont use global variables. Create them in main.
int isecret = 0; // read above.
int nog[20] = {}; // creates an array.
int i = 0; //This is the amount of rounds played.
describe_program();
cout << "If you want to play the game enter 'Y' if not enter 'N' to exist." << endl;
cin >> choice;
srand(time(0));
while (choice != 'N' && choice != 'n')
{
isecret = rand() % 100 + 1;
PlayGame(nog, i, isecret);
i++; // increases amount of rounds each round.
cout << "Do you want to play the game again" << endl;
cin >> choice;
}
cout << endl << nog[0] << endl << nog[1] << endl;
// This cout statement is just to show that you can print out the amount of guesses
// each round. If you play 3 rounds. The amount of guesses will be stored in
// nog[0], nog[1] and nog[2].
return(0);
}
Thank you so much for your help. This is exactly what he wants us to do except for a few problems with the code above. This would work if I was suppose to limit the amount of games a user plays, but since I'm not suppose to, according to this code above I would have to display all 20 elements in the array since I will never know how many times the user plays the game. I was thinking of using a counter in the array and increasing it as the user keeps playing, that way it prints out only for the amount of games the user plays. I got this far but i'm not sure how to actually print it out.
#include <iostream>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
int asize = 0;
int nog[20];
int isecret;
int i = 1;
int total = 0;
char play = 'g';
void describe_program()
{
cout << "The object of this game is to guess the random number that's in-between 1 and 100 in as few attempts as possible. ";
cout << "If you guess the number right you win the game, if not you can try until you get it right. ";
}
void PlayGame()
{
int guess;
cout << isecret << endl;
cout << "Enter a guess between 1 and 100\n";
do
{
cin >> guess;
i++;
if (guess < isecret)
{
cout << "Your guess is too low\n";
cout << "Try and guess again\n";
}
elseif (guess > isecret)
{
cout << "Your guess is too high" << endl;
cout << "Try and guess again" << endl;
}
else
{
cout << "Congradulations, you guess the number right\n";
nog[asize] = i;
}
asize++;
} while (guess != isecret);
}
int main()
{
char choice;
describe_program();
cout << "If you want to play the game enter 'Y' if not enter 'N' to exist." << endl;
cin >> choice;
srand(time(0));
while (play == 'g')
{
if (choice == 'Y' || choice == 'y')
{
isecret = rand() % 100 + 1;
PlayGame();
cout << "Do you want to play the game again" << endl;
cin >> choice;
}
elseif (choice == 'n' || choice == 'N')
break;
else
cout << "Invalid choice";
break;
}
}
Using your code, I can only play the game once, if I type in Y or y it still exists after one round.
Edit: Also, Im not sure if this is coming from your professor, but if he or she is letting you use global variables... jeez. Seriously stop using global variables.