Question
I need some help running this code. I can't get either of them to run properly. #include #include #include using namespace std; int getInt(string); int
I need some help running this code. I can't get either of them to run properly.
#include
#include
#include
using namespace std;
int getInt(string);
int getInt(string, int, int);
void main()
{
int number = 0;
number = getInt( "Enter a number from 1 to 10: " , 1, 10);
cout << number << endl;
system("pause");
}
int getint(string prompt)
{
string userInput;
int value = 0;
while (true)
{
cout << prompt;
getline(cin, userInput);
try
{
value = stoi(userInput);
break;
}
catch (exception ex)
{
cout << "***Error Integers only! Try again ";
}
}
return value;
}
int getInt(string prompt, int low, int high)
{
int value = 0;
value = getint(prompt);
while (value < low || value > high)
{
cout << "***ERROR Numbers from " << low << " to " << high << " only! ";
value = getInt(prompt);
}
return value;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#include
#include
#include
using namespace std;
int getInt(string);
int getInt(string, int, int);
int rollDice(int numSides, int numDice);
void main()
{
int numSides = getInt("How many sides does the dice have? ", 1, 100);
int numDice = getInt("How many dice to roll each time?", 1, 10);
int numRolls = getInt("Roll how many times?");
int roll = 0;
srand(time(0));
for (int x = 1; x <= numRolls; x++)
{
cout << "The roll is: " << rollDice(numSides, numDice) << endl;
}
system("pause");
}
int getInt(string prompt)
{
string userInput;
int value = 0;
while (true)
{
cout << prompt;
getLine(cin, userInput);
try
{
value = stoi(userInput);
break;
}
catch (exception ex)
{
cout << "***ERROR - Integers only! Try again ";
}
}
return value;
}
int getInt(string prompt, int low, int high)
{
int value = 0;
value = getInt(prompt);
while (value < low || value > high)
{
cout << "***ERROR - Numbers from " << low << " to " << high << " only! ";
value = getInt(prompt);
}
return value;
}
int rollDice(int numSides, int numDice)
{
int roll = 0;
for (int x = 1; x <= numDice; x++)
{
roll += rand() % numSides + 1;
}
return roll;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started