473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Adding Random Numbers help

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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib> 
  3. #include <ctime> 
  4. using namespace std;
  5.  
  6. void displayHeader();
  7. void problemType();
  8. void calcRealAnswer();
  9. bool goAgain();
  10.  
  11. int calculateRandomNumber();
  12.  
  13. int choice = 0;
  14. int userAnswer = 0;
  15. int realAnswer = 0;
  16. float randomNumber = 0;
  17.  
  18.  
  19. int main(){
  20.     srand((unsigned)time(0)); 
  21.  
  22. do {
  23.         displayHeader();
  24.         problemType();
  25.         }while(goAgain());
  26.         system("cls");
  27.  
  28. cin.get();
  29. cin.get();
  30. return 0;
  31. }
  32.  
  33. void displayHeader()
  34. {
  35.      cout<<"Welcome to my math game program. Sit back and enjoy!!"<<endl;
  36.      cout<<endl;
  37. }
  38.  
  39. void problemType()
  40. {
  41.  
  42.      cout << "Enter the number for the problem type desired:" << endl;
  43.      cout << "1.  Addition"  << endl;
  44.      cout << "2.  Subtraction"  << endl;
  45.      cout << "3.  Multiplication"  << endl;
  46.      cout << "Enter choice: ";
  47.      cin >> choice;
  48.  
  49.      if (choice==1){
  50.      cout << calculateRandomNumber();
  51.      cout << " + "; 
  52.      cout << calculateRandomNumber();
  53.      cout << " = ";
  54.      cin >> userAnswer;}
  55.  
  56.      else if (choice==2){
  57.      cout << calculateRandomNumber();
  58.      cout << " - ";
  59.      cout << calculateRandomNumber();
  60.      cout << " = ";
  61.      cin >> userAnswer;}
  62.  
  63.      else if (choice==3){
  64.      cout << calculateRandomNumber();
  65.      cout << " * "; 
  66.      cout << calculateRandomNumber();
  67.      cout << " = ";
  68.      cin >> userAnswer;}
  69.  
  70.      else{
  71.      cout << "Invalid answer. Please select a valid choice: ";
  72.      cin >> choice;
  73.      }
  74.  
  75. }
  76.  
  77. bool goAgain()
  78. {
  79.      char answer;
  80.  
  81.      cout << "Would you like to play again? ";
  82.      cin >> answer;
  83.  
  84.      system("cls");
  85.   while(answer != 'Y' && answer != 'N')
  86.   {
  87.   cout << "Invalid choice.  Would you like to go again? ";
  88.   cin >> answer;
  89.   }
  90.  
  91.   cout << endl;
  92.  
  93.  
  94.   if(answer == 'Y')
  95.   return true;
  96.   else
  97.   return false;
  98.     }
  99.  
  100. int calculateRandomNumber(){
  101.  
  102.     return (rand()%100) + 1;
  103. }
  104.  
Jan 15 '07 #1
4 1857
Ganon11
3,652 Expert 2GB
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():

Expand|Select|Wrap|Line Numbers
  1. int num1, num2;
  2. num1 = calculateRandomNumber();
  3. 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).
Jan 15 '07 #2
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2.

Expand|Select|Wrap|Line Numbers
  1. int calcRealAnswer(){
  2. if (choice==1){
  3. int realAnswer;
  4. int num1, num2;
  5. realAnswer = num1 + num2;
  6.  
  7. if (userAnswer == realAnswer){
  8. cout << "Correct!";
  9. }
  10.  
  11. else
  12. {
  13. cout << "Wrong!";}
  14. }
  15. }
  16.  
Jan 15 '07 #3
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?
Jan 15 '07 #4
Ganon11
3,652 Expert 2GB
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2.

Expand|Select|Wrap|Line Numbers
  1. int calcRealAnswer(){
  2. if (choice==1){
  3. int realAnswer;
  4. int num1, num2;
  5. realAnswer = num1 + num2;
  6.  
  7. if (userAnswer == realAnswer){
  8. cout << "Correct!";
  9. }
  10.  
  11. else
  12. {
  13. cout << "Wrong!";}
  14. }
  15. }
  16.  
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):

Expand|Select|Wrap|Line Numbers
  1. void problemType() {
  2.     int num1, num2;
  3.     num1 = calculateRandomNumber();
  4.     num2 = calculateRandomNumber();
  5.     int userChoice;
  6.     // Get user choice here: 1 = Add, 2 = Subtract, 3 = Multiply
  7.  
  8.     switch (userChoice) { // This is my preference, you can use an if...else ladder
  9.         case 1:
  10.             cout << num1 << " + " << num2 << " = ?" << endl;
  11.             int userGuess;
  12.             cin >> userGuess;
  13.             if (userGuess == num1 + num2) cout << "Correct!" << endl;
  14.             else cout << "Wrong!" << endl;
  15.             break;
  16.         case 2:
  17.             // Continue with subtraction, multiplication, etc.
  18.     }
  19. }
Jan 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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) ...
8
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) ...
8
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) ...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
12
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??...
1
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...
0
isladogs
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...
0
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...
0
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,...
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.