I have been working on this program that is a timed mathematical quiz and have everything completed. The problem is I keep getting warnings when I compile that says 1: 'sign' : not all control paths return a value and 2: 'equation' : not all control paths return a value. Can anyone help me see what the problem is?
Code:
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<cstdlib>
#include<ctime>
int num(); // function prototype
char sign(); // function prototype
int equation( int a, char c, int b ); // function prototype
int main()
{
srand( time( 0 ) ); // This time function returns the current
// "calendar time" in seconds. This value is
// converted to an unsigned integer and used as
// the seed to the random number generator.
int numcorrect = 0;
int stop = time( 0 ) + 30;
int x, y, answer, correctanswer;
char z;
while ( time( 0 ) < stop )
{
x = num(), y = num(); // Assignment of x and y to the num function
z = sign();
cout << "What is " << x << " " << z << " "
<< y << endl; // Prompt to ask the arithmetic question
cin >> answer; // Input answer
correctanswer = equation( x, z, y ); // Assignment of correctanswer
// to the equation function
if ( correctanswer == answer ) // tests user's answer
{
cout << "Very good!\n\n";
++numcorrect;
}
else
{
cout << "Incorrect.\n";
cout << "The correct answer is " << correctanswer << "\n\n";
}
} // end while loop
cout << "\nYou answered " << numcorrect
<< " questions correctly.\n\n"; // Prompts # of correct answers
return 0;
}
// Function implementation
int num()
{
return 1 + ( rand() % 12 ); // returns random integer between 1 and 12
}
// Function implementation
char sign()
{
int k = 1 + rand() % 5; // assigns random integers between 1 and 5 to k
if ( k == 1 ) // tests k and returns the + sign
return '+';
if ( k == 2 ) // tests k and returns the - sign
return '-';
if ( k == 3 ) // tests k and returns the * sign
return '*';
if ( k == 4 ) // tests k and returns the / sign
return '/';
if ( k == 5 ) // tests k and returns the % sign
return '%';
} // ends the sign function implementation block
// Function implementation
// a, c, and b below are parameters to
// the equation function implementation
int equation ( int a, char c, int b )
{
if ( c == '+' ) // tests c to return the appropriate equation
return a + b;
if ( c == '-' )
return a - b;
if ( c == '*' )
return a * b;
if ( c == '/' )
return a / b;
if ( c == '%' )
return a % b;
} // ends equation function implementation block
Thank you!
teeCee