0

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?

#include <iostream>
#include <cstdlib> 
#include <ctime> 
using namespace std;

void displayHeader();
void problemType();
void calcRealAnswer();
bool goAgain();

int calculateRandomNumber();

int choice = 0;
int userAnswer = 0;
int realAnswer = 0;
float randomNumber = 0;


int main(){
    srand((unsigned)time(0)); 

do {
        displayHeader();
        problemType();
        }while(goAgain());
        system("cls");

cin.get();
cin.get();
return 0;
}

void displayHeader()
{
     cout<<"Welcome to my math game program. Sit back and enjoy!!"<<endl;
     cout<<endl;
}

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;
     }
     
}

bool goAgain()
{
     char answer;

     cout << "Would you like to play again? ";
     cin >> answer;

     system("cls");
  while(answer != 'Y' && answer != 'N')
  {
  cout << "Invalid choice.  Would you like to go again? ";
  cin >> answer;
  }

  cout << endl;
  

  if(answer == 'Y')
  return true;
  else
  return false;
    }

int calculateRandomNumber(){
    
    return (rand()%100) + 1;
}
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by rinoa04
0

The problem is, i cant get the correct answer. It just says: Would you like to play again?

You don't have any code that does anything with the answer. All you do is print an expression and take user input in a conditional loop, so it's a little much to expect the program to do something you haven't told it to do. ;)

0

Yes, it just as what Ravalon said you don't have the coding that does anything to the answer. Therefore, it just ask the user "Would you like to play again" since no coding stating what happened after user given the answer and before asking the user to play again. All you need is to add a coding that check for the answer the user enter whether it is wrong or right answer given and display the message. Then it should be working. :-)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.