0

I am having trouble with generaetOutput function the void part is throwing me off. I tried to do it like the genrateQuestion function but the void part has me confused. Also my while statement is missing something because the error message continues to loop and if the user enters a wrong answer it generates another question without staying on the same question for the user to answer it. If you cut out from line 62 to 83 the program will run and you can see what i mean. The purpose of this program is to create a multiplication problem by generatiung two random numbers. The user then puts in the answer, if it is correct the app. displays one of three random messages; "Good Job",Excellent", and "very good". If the user enters the wrong answer it should reply with the prompt "NO. PLease try again (-1 to exit)" and repeat the same question till the user enters the correct answer or -1 to exit.

// Exercise 12.16: MultiplicationTeacher.cpp
// This game asks the user to enter the product of two
// randomly generated numbers in the range 0-9.
#include <iostream> // required to perform C++ stream I/O
#include <ctime>    // contains prototype for function time

// contains function prototypes for functions srand and rand
#include <cstdlib>

using namespace std; // for accessing C++ Standard Library members

// function prototypes
int generateQuestion();
void generateOutput();

// function main begins program execution
int main()
{
   // randomize random number generator using current time
   srand( time( 0 ) ); 

   // define variables
   int userAnswer = 0; // stores the user's answer
   int correctAnswer=0;  // stores the correct answer

while (userAnswer != -1 )
{
    {
    correctAnswer= generateQuestion ();
    cin >> userAnswer;

while (userAnswer != correctAnswer && userAnswer != -1)


    cout << "\nNo. Please try again ( -1 to end ) ?";
    }
    if(userAnswer==correctAnswer)
    {
        cout<<" ";
    }

}
   return 0; // indicates successful termination

} // end function main

//generates question function
int generateQuestion (void)
{
    int one;//stores value of first number
    int two; //stores value of second number
    int correctAnswer;//stores value of product of first and second number

    one= 1 + rand () %9;//picks random first number value
    two= 1 + rand () %9;//picks random second number value
    correctAnswer= one * two; //product of second number and first

    //display results
    cout << "\nHow much is " << one << " times " << two << "( -1 to end) ? " << endl;
    return correctAnswer;
}
//generates output function
void generateOutput( )
{
    int randomnumber;

    randomnumber=1+rand()%3;

    //display results
    if (randomnumber==1)
    {
        cout<<"Good Job!";
    }
    if (randomnumber==2)
    {
        cout <<"Excellent";
    }
    if (randomnumber==3)
    {
        cout<<"Very Good!";
    }

}   return randomnumber;

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ctaylo21
0

Your problem with the generateOutput() function is that you are returning randomnumber, which is of type int, but you have the return type of the function as void. So you are probably getting an error there. As a side note, please surround your code with the code tags like this:

// Exercise 12.16: MultiplicationTeacher.cpp
// This game asks the user to enter the product of two
// randomly generated numbers in the range 0-9.
#include <iostream> // required to perform C++ stream I/O
#include <ctime> // contains prototype for function time

// contains function prototypes for functions srand and rand
#include <cstdlib>

using namespace std; // for accessing C++ Standard Library members

// function prototypes
int generateQuestion();
void generateOutput();

// function main begins program execution
int main()
{
// randomize random number generator using current time
srand( time( 0 ) );

// define variables
int userAnswer = 0; // stores the user's answer
int correctAnswer=0; // stores the correct answer

while (userAnswer != -1 )
{
{
correctAnswer= generateQuestion ();
cin >> userAnswer;

while (userAnswer != correctAnswer && userAnswer != -1)


cout << "\nNo. Please try again ( -1 to end ) ?";
}
if(userAnswer==correctAnswer)
{
cout<<" ";
}

}
return 0; // indicates successful termination

} // end function main

//generates question function
int generateQuestion (void)
{
int one;//stores value of first number
int two; //stores value of second number
int correctAnswer;//stores value of product of first and second number

one= 1 + rand () %9;//picks random first number value
two= 1 + rand () %9;//picks random second number value
correctAnswer= one * two; //product of second number and first

//display results
cout << "\nHow much is " << one << " times " << two << "( -1 to end) ? " << endl;
return correctAnswer;
}
//generates output function
void generateOutput( )
{
int randomnumber;

randomnumber=1+rand()%3;

//display results
if (randomnumber==1)
{
cout<<"Good Job!";
}
if (randomnumber==2)
{
cout <<"Excellent";
}
if (randomnumber==3)
{
cout<<"Very Good!";
}

} return randomnumber;
This question has already been answered. 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.