jeudi 17 novembre 2016

C++ Number Guessing Game: Higher or Lower

I have to write a program for a guessing game. The user starts with the number 5, and has to guess if the next randomly generated number between 1-10 will be higher or lower. If they guess correctly, the game can go on continuously but if they make three mistakes the game is over. I'm having trouble because with my current code, it only generates one random number instead of a new one after each attempt. Any help would be immensely appreciated!

#include <iostream>
#include <time.h>       /* time */

using namespace std;


int main()
{
        cout << "This is a very simple number guessing game. Each time you will be given a number of the range 0-10." << endl;
        cout << "The objective of the game is to guess whether the next number is going to be higher or not. As simple as that." << endl;
        cout << "You have the ability to make no more than 3 incorrect guesses before you lose, so guess wisely." << endl << endl;
        cout << "You are starting with number 5. Is the next number higher(write H) or lower(write L) ?" << endl;

    int incorrectGuesses = 0;
    int correctGuesses;
    int prevNum = 5, nextNum;

    //Declare response to user inputs
    string CorrectStatement = "Correct! The new number is "; //Turning text string for correct guesses into variable
    string IncorrectStatement = "Wrong, you made an incorrect guess! The new number is "; //Turning text string for incorrect guesses into variable


    // Appropriate Loop structure
    srand ( time(NULL) );
    nextNum = rand()%10+0;
    do{


        // appropriate variable initialization
        char choice;

        cin >> choice;

        if (choice == 'H')
        {
            if (prevNum < nextNum) 
                cout << CorrectStatement << nextNum << endl;
                correctGuesses++;


            if (prevNum > nextNum)
                cout << IncorrectStatement << nextNum << endl;
        }

        if (choice == 'L')
       {    
            if (prevNum > nextNum) 
                cout << CorrectStatement << nextNum << endl;
                correctGuesses++;
            if (prevNum < nextNum)
                cout << IncorrectStatement << nextNum << endl;
                incorrectGuesses++;
        }
    } while (incorrectGuesses < 3);







    if (incorrectGuesses == 3)
    {
        cout << "You've made 3 incorrect guesses ! Game is now over !" << endl;
        cout << "You had " << correctGuesses << " correct guesses before the game was over" << endl;
    }

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire