Number Guessing Game, but game plays with out player...
Oct 21, 2016 at 3:25am UTC
I am programming this Number Guessing Game to work on my skills, the only problem? Is that the game runs all the way through after starting with out player input.
I'm not really asking for someone to show me everything I am doing wrong, unless that would be easier, more just a hint in the right direction.
Code Below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void playGame();
int main()
{
cout << "Hello" << endl;
Sleep(1500);
cout << "Are you ready to play?" << endl;
char input;
cin >> input;
input = toupper(input);
if (input == 'Y' ) {
cout << input << endl;
}
else if (input == 'N' ) {
cout << input << endl;
exit(1);
}
else {
cout << "Please try again!" << endl;
exit(1);
}
playGame();
return 0;
}
void playGame()
{
int guessCount = 3;
for (int i = 0; i < 200; i++) {
cout << endl;
}
for (int i = 0; i < guessCount; i++) {
cout << flush;
if (guessCount > 1) {
cout << "You have " << guessCount << " guesses left!" << endl;
}
if (guessCount == 1) {
cout << "You have " << guessCount << " guess left!" << endl;
}
srand(time(NULL));
int randomNumber = rand() % 100 + 1;
cout << "Guess a number between 1 and 100" << endl;
char guessed[3];
cin >> guessed;
int guess = (int )guessed;
if (guess == randomNumber) {
cout << "YOU GUESSED CORRECTLY!" << endl;
if (guessCount > 1) {
cout << "YOU HAD " << guessCount << " GUESSES LEFT!" << endl;
}
else if (guessCount == 1) {
cout << "YOU HAD " << guessCount << " GUESS LEFT!" << endl;
}
Sleep(3000);
exit(1);
}
if (guess != randomNumber) {
cout << "You didn't guess correctly!\n" << endl;
guessCount -= 1;
if (guessCount > 1) {
cout << "You have " << guessCount << " guesses left!\n" << endl;
}
else if (guessCount == 1) {
cout << "You have " << guessCount << " guess left!\n" << endl;
}
if (guess > randomNumber) {
cout << "You guessed too high!\n" << endl;
}
else {
cout << "You guessed too low!\n" << endl;
}
if (guessCount == 0) {
cout << "You don't have any more guesses! The correct answer was " << randomNumber << "!\n" << endl;
Sleep(3000);
exit(1);
}
}
}
}
Last edited on Oct 21, 2016 at 3:26am UTC
Oct 21, 2016 at 3:27am UTC
Please don't use Sleep(). It is not universal and cross-platform.
Oct 21, 2016 at 3:32am UTC
SakurasouBusters
Please don't use Sleep(). It is not universal and cross-platform.
Okay but what would you suggest I use instead?
Oct 21, 2016 at 3:39am UTC
I wrote this function myself.
1 2 3 4 5 6 7 8 9 10 11 12
void delay(int milliseconds)
{
clock_t start_t = clock();
while (true )
{
clock_t end_t = clock();
double timeElapsed = ((double )(end_t - start_t) / (double )CLOCKS_PER_SEC) * 1000;
if (timeElapsed >= milliseconds) return ;
}
}
You need to include the header #include <ctime>
Oct 21, 2016 at 4:00am UTC
If your compiler uses C++'11 or more recent you can use sleep_for
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
std::cout << "Sleep for 3 seconds...\n" ;
std::this_thread::sleep_for(std::chrono::seconds(3));
}
Topic archived. No new replies allowed.