View Full Version: Adding random numbers help

C++ Learning Community > C++ Help > Adding random numbers help


Title: Adding random numbers help


sin08 - January 9, 2007 08:06 PM (GMT)
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?

CODE

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

Shonoby - January 9, 2007 09:51 PM (GMT)
Ok, nvm, the new things i found out about ur code was that since u have a function specifically to generate some random numbers every time u call it. But since it is random every time the previous code would not work because it had new values instead of the ones alrdy used to tell u the problem:
(previous code)
CODE

if( userAnswer == ( calculateRandomNumber() + calculateRandomNumber() ) )
        cout << "You RIGHT!!\n";
    }
else
        cout << "Your wrong!!\n";

This code will not work, so what u should do is instead of making a function to find the rand() make two variables that both equal (= (rand() % 100; //or whatever # ), and instead of calling the function just call the two variables like shown in the code below. So after the user inputs 'choice', there should be a switch struc. that does everything you were doing into one step. Then u can add any other functions or whatever needed.
CODE

switch ( choice ) {
case 1:
cout << variable1 << ' + ' << variable2 << ' = ';
cin >> userAnswer;
if( userAnswer == ( variable1 + variable2 ) )
        cout << "You RIGHT!!\n";
    }
else
        cout << "Your wrong!!\n";
break;
............
}

Ohh and also the code u put for srand was correct but also unnecessary well part of it because:
This was how u had it:
CODE

srand((unsigned)time(0));

Correct as it is but there is no need for the (unsigned) because time is an unsigned value. So it should be like this:
CODE

srand(time(0));




Hosted for free by InvisionFree