I need to create a program where the user selects addition, subtraction, or multiplication, and when they select one, 2 random numbers appear, and they must put in the right answer. The problem is, i cant get the correct answer. It just says: Would you like to play again? 4 1857
void problemType()
{
cout << "Enter the number for the problem type desired:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice==1){
cout << calculateRandomNumber();
cout << " + ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}
else if (choice==2){
cout << calculateRandomNumber();
cout << " - ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}
else if (choice==3){
cout << calculateRandomNumber();
cout << " * ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}
else{
cout << "Invalid answer. Please select a valid choice: ";
cin >> choice;
}
}
int calculateRandomNumber(){
return (rand()%100) + 1;
}
[/code]
Your problem is with your problemType() function. You never do anything. Suppose the user hits 1 for Addition. Your code outputs the random number plus another random number, and then asks for the answer. But it never does anything with the answer! You need to check and see if, in fact, their answer is correct. In order to do this, you will have to store the random numbers you generate. Since calculateRandomNumber returns an integer, you should store that integer into a value. For instance, after the user enters 1 for addition, make two int variables (num1 and num2) and set these variables to the value returned by calculateRandomNumber(): - int num1, num2;
-
num1 = calculateRandomNumber();
-
num2 = calculateRandomNumber();
Now you can check to see if the value the user enters is equal to num1 + num2 (or minus, or multiplied, depending on the block you're in).
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2. -
int calcRealAnswer(){
-
if (choice==1){
-
int realAnswer;
-
int num1, num2;
-
realAnswer = num1 + num2;
-
-
if (userAnswer == realAnswer){
-
cout << "Correct!";
-
}
-
-
else
-
{
-
cout << "Wrong!";}
-
}
-
}
-
My bad, there were 2 equal signs :)
BUT is there a way i can make it so that after 3 guesses, it disables the user from entering anything else and it displays the real answer?
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2. -
int calcRealAnswer(){
-
if (choice==1){
-
int realAnswer;
-
int num1, num2;
-
realAnswer = num1 + num2;
-
-
if (userAnswer == realAnswer){
-
cout << "Correct!";
-
}
-
-
else
-
{
-
cout << "Wrong!";}
-
}
-
}
-
In this case, you are still not initializing num1 and num2. Thus, realAnswer is going to have some garbage value, since you don't know what will be in num1 and num2. Here's what you should have (or something like this): - void problemType() {
-
int num1, num2;
-
num1 = calculateRandomNumber();
-
num2 = calculateRandomNumber();
-
int userChoice;
-
// Get user choice here: 1 = Add, 2 = Subtract, 3 = Multiply
-
-
switch (userChoice) { // This is my preference, you can use an if...else ladder
-
case 1:
-
cout << num1 << " + " << num2 << " = ?" << endl;
-
int userGuess;
-
cin >> userGuess;
-
if (userGuess == num1 + num2) cout << "Correct!" << endl;
-
else cout << "Wrong!" << endl;
-
break;
-
case 2:
-
// Continue with subtraction, multiplication, etc.
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
...
|
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
...
|
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
...
|
by: Sri |
last post by:
How do you add an n-bit number in C?
Regards,
Sri
|
by: Naya |
last post by:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total??...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |