mercredi 5 avril 2017

What is wrong with my do...while logic, and continue logic?

I'm new to stackoverflow, and also somewhat new to programming, so please don't mind my poor formatting of the code. I have two problems with my code.

  1. My continue statement, which I'm using to continue the loop if the player types 'y' or 'Y', doesn't work. It terminates the program after only getting the guess correctly, which leads me to:

2.My continue counter goes past 0 without stopping, and I just can't see my error in the logic of the program.

I can't see the problems with my logic.

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <random>

    using namespace std;

    int getNumber(); //random number prototype
    double getScore(); //gets score
    int chances = 7; //chances to guess with
    int main()
    {
    int guess = 0, 
        random;
    char retry = 'y'; //initialize retry to 'y'
    cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
     << "You have 7 chances. Good luck! \n \n" << endl;


    random = getNumber(); //give the function a variable

  do
  {

    cout << random << "\n" << "\n";
    chances--;

    cout << "Enter your guess: ";
    cin >> guess;

        if (guess == random)
        {
            cout << "You have won the game! " << "Your score was: " << getScore();

            cout << "Would you like to retry? (Y or N): ";
            cin >> retry;

            if (retry == 'y' || 'Y')
            {
                chances = 7;
                getNumber();
                continue; //player can retry the game
            }
            else if (chances == 0)
            {
                cout << "You have no chances left. Retry? (Y or N): ";
                    cin >> retry;
                if (retry == 'y' || 'Y')
                {
                    chances = 7;
                    getNumber();
                    continue;
                }
            }

                return 0;
        }
        else if (guess != random)
            cout << "You got it wrong. \n" << "You have: " << chances << " chances left" << endl << endl;
        else
            cout << "Incorrect Input. Please type a number." << endl << endl;
   } while (guess != random);


return 0;
    }

    int getNumber()
    {
     unsigned seed = time(0); //seed the random number
     srand(seed);

     int randNum = rand() % 10 + 1; //random number in the range of 1-10
     return randNum;
    }




Aucun commentaire:

Enregistrer un commentaire