Question
I have this C++ code for lottery, but it handles the situation wrong where the user inputs a negative value. #include using namespace std; unsigned
I have this C++ code for lottery, but it handles the situation wrong where the user inputs a negative value.
#include
using namespace std;
unsigned long int read_input(string question) {
unsigned long int user_input;
while (true) {
cout
cin >> user_input;
if (user_input > 0) {
return user_input;
} else {
cout
}
}
}
unsigned long int factorial(unsigned long int i) {
unsigned long int k = 1;
while (i > 0) {
k *= i;
i -= 1;
}
return k;
}
unsigned long int calculate_probability(unsigned long int n, unsigned long int p) {
if(p == 0) return 1;
return factorial(n) / (factorial(p) * factorial(n - p));
}
int main() {
unsigned long int balls_tot = read_input("Enter the total number of lottery balls: ");
unsigned long int ball_count = read_input("Enter the number of drawn balls: ");
if (ball_count > balls_tot) {
cout
} else {
cout
}
return 0;
}
It should work like this:
Model output: Enter the total number of lottery balls: 3 Enter the number of drawn balls: 4 The number of balls must be a positive numberStep 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