My professor has assigned a program that rolls two Dice. I am pretty much done coding it I just can not get it to display a message when I enter a number less than 2 or more than 12. It is a requirement in the assignment to display a message like: "The sum falls outside parameters." when a user types an integer more than 12 and less than 2. I am using if statements but I have no idea what I am doing wrong I just need this message to appear please help!
// Begin function
int main()
{
cout << "Enter the desired sum of the numbers to be rolled: ";
cin >> num;
cout << "Enter the amount of times the dice are rolled to get your desired sum: "
<< rollDice(num) << endl;
if (num >= 2 && num <=12)
{
cout << "The sum is between 2 and 12! " << endl;
}
else if (num <2)
cout << "The sum falls outside parameters." << endl;
else if (num > 12)
{
cout << "The sum falls outside parameters." << endl;
}
return 0;
}
int rollDice(int num)
{
int die1;
int die2;
int sum;
int rollCount = 0;
// Begin function
int main()
{
while(
cout << "Enter the desired sum of the numbers to be rolled: " &&
cin >> num &&
(num < 2 || num > 12)
)
{
cout << "Invalid number - must be >= 2 or <= 12\n";
}
cout
<< "Enter the amount of times the dice are rolled to get your desired sum: "
<< rollDice(num) << endl;
return 0;
}
And if you just want to stick to what you had and work with that then the sequence of your code needed a small adjustment (only). The only difference is the while loop gives the user a chance to recover any input error.
//pre-proccessing directives
#include <iostream>
#include <cstdlib>
#include <ctime>
//standard library
usingnamespace std;
// Standard library
usingnamespace std;
// Declare local variables
int num;
int rollDice(int num);
// Begin function
int main()
{
cout << "Enter the desired sum of the numbers to be rolled: ";
cin >> num;
if (num >= 2 && num <=12)
{
cout << "The sum is between 2 and 12! " << endl;
}
elseif (num <2)
cout << "The sum falls outside parameters." << endl;
elseif (num > 12)
{
cout << "The sum falls outside parameters." << endl;
}
cout << "The amount of times the dice are rolled to get your desired sum: "
<< rollDice(num) << endl;
return 0;
}
int rollDice(int num)
{
int die1;
int die2;
int sum;
int rollCount = 0;
srand(static_cast<unsignedint>(time(0)));
do
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}
while (sum !=num);
return rollCount;
}