2
// 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.

amanuel2
  • 4,508
  • 4
  • 36
  • 67
Xor
  • 41
  • 1
  • 6
  • 1
    Use a debugger. Step through your code to see where it does something different from what you want it to do. – Kerrek SB Oct 06 '16 at 23:59
  • 1
    And learn good practices. Like Formatting code which increases readability , and not [using namespace std](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)! – amanuel2 Oct 07 '16 at 00:09
  • 1
    @amanuel2, formatting is poor because I'm not sure how to properly format the code, when I pasted it, parts were missing so I had to space it 4 times so it would fit in the code block i'd assume – Xor Oct 07 '16 at 00:16
  • 1
    Visual Studio has a top-notch debugger. The sooner you get used to using it the sooner you can reap the productivity rewards. – user4581301 Oct 07 '16 at 00:24
  • 1
    Xor Just Requested edit to improve your formating . @user4581301 true words – amanuel2 Oct 07 '16 at 00:25

3 Answers3

1

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);
Emi Höss
  • 86
  • 4
0

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.

0

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