#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
usingnamespace 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: ";
}
elsebreak;
}
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!";
}
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.
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.