I need help with my Dice program

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!

//pre-proccessing directives
#include <iostream>
#include <cstdlib>
#include <ctime>

//standard library
using namespace std;

// Standard library
using namespace 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;

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;


srand(static_cast<unsigned int>(time(0)));

do
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}

while (sum !=num);

return rollCount;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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;
} 
closed account (48T7M4Gy)
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.

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
//pre-proccessing directives
#include <iostream>
#include <cstdlib>
#include <ctime>

//standard library
using namespace std;

// Standard library
using namespace 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;
    }
    
    else if (num <2)
        
        cout << "The sum falls outside parameters." << endl;
    
    else if (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<unsigned int>(time(0)));
    
    do
    {
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1;
        sum = die1 + die2;
        rollCount++;
    }
    
    while (sum !=num);
    
    return rollCount;
}
Topic archived. No new replies allowed.