Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c++ - 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! 
 
" << endl;


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

  do
  {

    cout << random << "
" << "
";
    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' || retry == 'Y')
            {
                chances = 7;
                guess = 0;
                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' || retry == 'Y')
                {
                    chances = 7;
                    guess = 0;
                    getNumber();
                    continue;
                }
            }

                return 0;
        }
        else if (guess != random)
            cout << "You got it wrong. 
" << "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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
if (retry == 'y' || 'Y')

This is incorrect logic, which is why your code does not work the way you want it to. You want it to be:

if (retry == 'y' || retry == 'Y')

Fix this logic error in your other if-else statements as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...