0

My program works, but I would like for the numbers to be random instead of 1-12...

I know I have to do srand and I want the numbers to at least go to 50, so like a rand()%50, but I'm not really sure where to plug it in, if thats right.

thanks for all your help

here is my code:

#include <ctime>
#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;


int main()
{
        int product;
        int sum;
        int answer;
        int correct = 0;
        int wrong = 0;
        int menu;

         srand(time(0));


        do {
                cout << setw( 55 ) << "Childrens Math Quiz Game!!!" << endl;
                cout << setw( 47 ) << "Author: Preston" << endl << endl;
                cout << setw( 49 ) << "**********Menu**********" << endl;
                cout << setw( 50 ) << "[1] Multiplacation " << endl;
                cout << setw( 44 ) << "[2] Addition " << endl;
                cout << setw( 47 ) << "[3] Subtraction " << endl;
                cout << setw( 40 ) << "[4] Exit " << endl << endl;
                cout <<"Please enter your choice: ";
                cin >> menu;

                switch ( menu )
                {
                case 1:
                        for ( int i = 1; i <= 12; i++ )

                                for( int j = 0; j <= 12; j++ ){

                                        cout << "What is " << i << " X " << j << " = ";
                                        cin >> answer;
                                        product = i * j;

                                        if( answer == product ) {
                                              cout <<"Good Job!!!!!" << endl << endl;
                                                correct++;
                                        }
                                        else {
                                                cout << "Sorry " << i <<" X " << j << " = " << product<< endl << endl;
                                                wrong++;
                                        }
                                }

                                cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
                                cin.get();
                                break;

                case 2:
                        for ( int i = 1; i <= 12; i++ )

                                for( int j = 0; j <= 12; j++ ){

                                        cout << "What is " << i << " + " << j << " = ";
                                        cin >> answer;
                                        sum = i + j;

                                       if( answer == sum ) {
                                                cout <<"Great Job!!!!!" << endl << endl;
                                                correct++;
                                        }
                                        else {
                                                cout << "Sorry " << i <<" + " << j << " = " << sum <<endl << endl;
                                                wrong++;
                                        }
                                }

                                cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
                                cin.get();;
                                break;

                case 3:
                        for ( int i = 12; i <= 22; i++ )

                                for( int j = 1; j <= 12; j++ ){

                                        cout << "What is " << i << " - " << j << " = ";
                                       cin >> answer;
                                        sum = i - j;

                                        if( answer == sum ) {

                                        cout <<"Terrific!!!!!" << endl << endl;
                                                correct++;
                                        }
                                        else {
                                                cout << "Sorry " << i <<" - " << j << " = " << sum <<endl << endl;
                                                wrong++;
                                        }
                                }

                                cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
                                cin.get();
                                break;

                default:
                        break;


                }

        }while ( menu != 4 );
        return 0;
}
2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by danzona
0

Just replace the i and j loops to call rand().

For example, in the multiplication loop change:

case 1:
                        for ( int i = 1; i <= 12; i++ )

                                for( int j = 0; j <= 12; j++ ){

                                        cout << "What is " << i << " X " << j << " = ";
                                        cin >> answer;
                                        product = i * j;

                                        if( answer == product ) {
                                              cout <<"Good Job!!!!!" << endl << endl;
                                                correct++;
                                        }
                                        else {
                                                cout << "Sorry " << i <<" X " << j << " = " << product<< endl << endl;
                                                wrong++;
                                        }
                                }

to

case 1:
                        i = rand()%50;
                        j = rand()%50;
                        cout << "What is " << i << " X " << j << " = ";
                        cin >> answer;
                        product = i * j;

                        if( answer == product ) {
                              cout <<"Good Job!!!!!" << endl << endl;
                                correct++;
                        }
                        else {
                                cout << "Sorry " << i <<" X " << j << " = " << product<< endl << endl;
                                wrong++;
                        }
                        cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
                        cin.get();
                        break;

Don't forget to get rid of the extra set of {} when you make the conversions.

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.