Why does my code ask for "Please enter a number between 2-12: " twice?

Here is the whole code if it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <ctime>
#include <cstdlib>
//This program prompts the user to select a number between 2 and 12
//computer throws two dices
//player win if selected number matches the sum of the two dices thrown by computer
//for this Wednesday complete program by:
//add a module/function that throws first dice
//add a module/function that throws second dice
//add a function that evaluate game (comparing user selection with sum of the two dices)
//display outcome
//prompt the user if he/she wants to play again or quit
using namespace std;


int user();
int throwdice();
int Evaluate();

int main()
{
    int userchoice = 0;
    int Dice = 0;
    int Display = 0;
    do
    {
        userchoice = user();
        Dice = throwdice();
        Display = Evaluate();
    }
    while (Display == 1);

}

int user()
{
    int usernumber = 0;
    cout << "Please enter a number between 2-12: ";

while (true)
        {
            cin >> usernumber;
            if (usernumber < 2 || usernumber > 12)
                {
                cout << "Error! \n";
                cout << "Please enter a number between 2-12: ";
                }
                else
                    break;
        }

return usernumber;
}

int throwdice()
{
    int sum = 0;
    int Dice = 0;
    int Dice2 = 0;

    srand(time(0));
    Dice = rand()%6+1;
    Dice2 = rand()%6+1;
    sum = Dice + Dice2;
    return sum;
}

int Evaluate()
{
    int Dice = throwdice();
    int userchoice = user();
    int playagain = 0;

    cout << "You entered: " << userchoice << endl << "The computer has: " << Dice << endl;
    if (userchoice == Dice)
        cout << "YOU WIN!!!!";
        else
        {
            cout << "You lose.";
        }
    cout << "\nWanna play again? 1 for yes, anything else equals no.: ";
    cin >> playagain;
    if (playagain == 1)
        return 1;
    else
        cout << "Goodbye!";

}

Last edited on
Please use

[code ]... [ /code]

and please indent. :)
@thehunter629

Why does my code ask for "Please enter a number between 2-12: " twice?


Because you ask the question twice. The first time was after you initialized usernumber as zero, and the second time after you check if the number is less than 2, which it is.

PS There should be NO spaces between the square brackets. Just the word code and the forward slash code.
Last edited on
You need to use [code][/code] without any spaces for it to work properly.
@whitenite1

Please reread the code because even if the second:
"cout << "Please enter a number between 2-12: ";"

is removed it still prompts the user twice.
The error is in int main()

This:
userchoice = user();
Dice = throwdice();

Is no needed to loop the code.
@thehunter629

Sorry for my error, but I based my response on just the snippit of code that you had posted previously. My answer would have been different if the whole code was shown, as you have posted now.
Topic archived. No new replies allowed.