简体   繁体   中英

How to evaluate number greater than and less than in the same while loop?

// DiceRollProject.cpp : Defines the entry point for the console     application.
//

#include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;

int diceRoll(int max);  // function definition
int getValidInteger();// function definition

int main() {

    srand(time(0)); // seed the random number generator

    int exitProgram = 0;
    int guess, rollValue;
    int maxRollValue = 6;
    cout << "Hello! Let's play a dice game. Let me do the first roll for you.\n" << endl;
    rollValue = diceRoll(maxRollValue);
    cout << "In this roll, you got: " << rollValue << "\n" << endl;

    do {
        rollValue = diceRoll(maxRollValue);


        cout << "What's your guess for the next roll? Enter an integer between 1 and " << maxRollValue << ": ";
        guess = getValidInteger();
        // TODO: Validate input

        if (guess > rollValue)
        {
            cout << "The guess was too high!";

        }

        if (guess < rollValue)
        {
            cout << "The guess was too low!";

        }

        if (guess == rollValue)
        {
            cout << "You guessed correctly, congrats!";

        }

        cout << "In this roll, you got: " << rollValue << "\n" << endl;
        // TODO: Evaluate result


        cout << "Enter 1 to exit or any other integer to continue rolling ";
        exitProgram = getValidInteger();
        cout << "\n";
        if (exitProgram == 1)
        {
            cout << "Sorry to see you go. Have a wonderful day!\n" << endl;
        }

    } while (exitProgram != 1);

    return 0;
}

// Roll the die
int diceRoll(int max) {
    int rollValue;

    rollValue = (rand() % max) + 1;

    return rollValue;
}


// Check if user entered an integer
int getValidInteger() {
    int userInput;

    cin >> userInput;

    while (userInput < 1)  {

        if (userInput < 1)
        {
            cout << "Please enter a number greater than or equal to 1\n";
        }

        if (userInput > 6)
        {
            cout << "Please enter a number less than or equal to 6\n";
        }

    }


    if (cin.fail()) {
        cin.clear();
        cin.ignore();
        cout << "Please enter an Integer only ";
        cin >> userInput;
        cout << "\n";
    }

    return userInput;
}

I have a dice roll guessing game, I'm trying to evaluate the users input, to make sure that they can't enter a number less than 1 and greater than 6, unfortunately, with just my if statements, they can still enter these numbers, although a string is displayed that the input is not valid, I want to make a while loop that keeps asking them to enter a valid number equal or greater than 1 and equal to and less than 6, if the user keeps inputting an incorrect number, the while loop will keep asking them for a valid number, until they do enter one, which will then run the program as normally.

First of all, inside the while loop you have dead code.

while (userInput < 1)  {

    if (userInput < 1)
    {
        cout << "Please enter a number greater than or equal to 1\n";
    }

    if (userInput > 6)
    {
        cout << "Please enter a number less than or equal to 6\n";
    }

}

Within the loop body, the first if is always true and the second one is always false. You should enter in a loop when the user writes an invalid input. This happens when (userInput < 1 or userInput > 6)

After the evaluation of the while's condition, you should ask the user to write input

do  {
    cout << "Please enter an Integer only ";
    cin >> userInput;
    if (userInput < 1)
    {
        cout << "Please enter a number greater than or equal to 1\n";
    }

    if (userInput > 6)
    {
        cout << "Please enter a number less than or equal to 6\n";
    }

}while(userInput < 1 || userInput > 6);

So your condition that will keep you in the while loop is if the person guesses too high or too low. Inside the while loop I would add the updating condition or statement that you would like to repeat. So in your case, "your guess is too high" or " your guess is too low" and ask for their input again. I am not a pro but I would keep it simple by constructing 2 while loops, one for too high and one for too low just like your if statements. literally you can just change your first two if statements to while loops and adding an few extra lines of cout to ask the person to guess again and validate their input. I hope this helped.

from what I've understood you are looking for something like this:

int main (){
int my_magic_number=(rand()%6)+1,usernumber=-1;
bool state;
while (usernumber!=my_magic_number){
     cin>>usernumber;
     state = (usernumber<1||usernumber>6);
     while (state) {
        cout<<"You entered a number outside the range [1,6] please try again\n";}
        cin>>usernumber;
        state = (usernumber<1||usernumber>6);
     }
    if (usernumber!=my_magic_number) {/* do whatever you want */}
} //while loop
} // main 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM