I thought I had worked it out, but no luck. Correct answers are sometimes output as wrong, and I still have no way of actually inputting operator into the question it asks the user. Getting closer though maybe. Revised code:
Code:
#include <iostream>
float divide (float x, float y); //function to divide numbers
int multiply (int x, int y); //function to multiply numbers
int add (int x, int y); //function to add numbers
int subtract (int x, int y); //function to subtract numbers
int generateRandomNumber(); //generates random number which will then be used as a condition to use an operator i.e. if 3, then multiply, if 4, then divide.
int main()
{
int x; //local variable to store users first chosen number
int y; //local variable to store users second chosen number
int userGuess; //local variable to store the users attempt at answering question
int answer; //local variable to store asnwer to random math question
char anotherGo = '1';
do
{
std::cout << "Please enter a number: ";
std::cin >> x;
std::cout << "Please enter another number: ";
std::cin >> y;
int generateOperator = generateRandomNumber(); //generates random number between 1-4, used in following conditions to trigger different functions
if (generateOperator == 1)
{
answer = divide(x,y);
}
else if (generateOperator == 2)
{
answer = multiply(x,y);
}
else if (generateOperator == 3)
{
answer = add(x,y);
}
else
{
answer = subtract(x, y);
}
std::cout << "What is " << x << " " << generateRandomNumber() << " " << y << "? "; //this line may not help to find the answer, have to check program for what the number printed corresponds to, i.e. add, multiply...!
std::cin >> userGuess;
std::cout << std::endl;
if (userGuess != answer)
{
std::cout << "Sorry, that's the wrong answer.";
}
else
{
std::cout << "Correct!";
}
std::cout << "\nDo you want another go? 1 for yes, 0 for no: ";
std::cin >> anotherGo;
std::cout << std::endl;
if (anotherGo == '0')
{
anotherGo = '0';
}
} while (anotherGo == '1');
std::cout << "Exit program";
}
float divide (float x, float y) //function to divide numbers
{
return x / y;
}
int multiply (int x, int y) //function to multiply numbers
{
return x * y;
}
int add (int x, int y) //function to add numbers
{
return x + y;
}
int subtract (int x, int y) //function to subtract numbers
{
return x - y;
}
int generateRandomNumber()
{
int randomNumber = rand() % 5;
return randomNumber;
}
Stopping for the night now so if anyone could take a look at this latest one and maybe suggest why it doesn't work as expected, I'd very much appreciate that. Thanks.