
Daniel C.
asked 02/25/14can anyone tell me why this has errors?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); //Random Number Generator
double value1 = rand() % 50 + 1; //Random number between 1 and 50
double value2 = rand() % 50 + 1;
int operatorDefine = rand() % 4 + 1; // random number between 1 and 4 to determine random operator
char operator1;
double answer;
int defineObject1 = rand() % 3 + 1;
int defineObject2 = rand() % 3 + 1;
double defineObject3;
int guess;
if (operatorDefine == 1) // if-else to decide random operator
{
operator1 = '+';
}
else if (operatorDefine == 2)
{
operator1 = '-';
}
else if (operatorDefine == 3)
{
operator1 = '*';
}
else if (operatorDefine == 4)
{
operator1 = '/';
}
if (operator1 == '+') // if-else to solve the random equation
{
(answer = value1 + value2);
}
else if (operator1 == '-')
{
(answer = value1 - value2);
}
else if (operator1 == '*')
{
(answer = value1 * value2);
}
else if (operator1 == '/')
{
(answer = value1 / value2);
}
cout << "Welcome to Guess My Number, with a twist!\n\n";
if (defineObject1 == 1) //if else to display the first piece of equation
{
cout << "Value 1 is " << value1 << endl;
}
else if (defineObject1 == 2)
{
cout << "Value 2 is " << value2 << endl;
}
else if (defineObject1 == 3)
{
cout << "The answer is " << answer << endl;
}
if (defineObject2 == 1) //if else to display the second piece of the equation
{
cout << "Value 1 is " << value1 << endl;
}
else if (defineObject2 == 2)
{
cout << "Value 2 is " << value2 << endl;
}
else if (defineObject2 == 3)
{
cout << "The answer is " << answer << endl;
}
if (defineObject1 == 1 && defineObject2 == 2) //if else to determine what the player needs to guess
{
(defineObject3 = answer);
}
else if (defineObject1 == 2 && defineObject2 == 1)
{
(defineObject3 = answer);
}
else if (defineObject1 == 1 && defineObject2 == 3)
{
(defineObject3 = value2);
}
else if (defineObject1 == 3 && defineObject2 == 1)
{
(defineObject3 = value2);
}
else if (defineObject1 == 2 && defineObject2 == 3)
{
(defineObject3 = value1);
}
else if (defineObject1 == 3 && defineObject2 == 2)
{
(defineObject3 = value1);
}
cout << "The operation is " << operator1 << endl;
cout << "Input the missing piece of the equation." << endl; //Retrieve player's guess
cin >> guess;
while (guess != defineObject3) // loop to determine if the player was correct
{
cout << "Sorry, but that is incorrect. Please try again." << endl;
cin >> guess;
}
else
{
cout << "Good Job, that is CORRECT!" << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
2 Answers By Expert Tutors
I see no point in analyzing spaghetti code like this. If you cannot easily debug a section of code, you need to push some of the details into functions that can be separately tested.
I second the use of an enumeration for the operator but would add a character value to it to hold '+' and the like. I also recommend the use of functions to push some of the details out of main. for example, the function gerResult would have arguments firstOperand, operatorEnum, and secondOperand; It would return the result of applying the operator to the operands. Variations to solve firstOperand operator secondOperand = result for any of its components would also be useful. For example getOperator( firstOperand, secondOperand, result) would return an operatorEnum. Then the main program looks like this:
Generate random values of firstOperand and SecondOperand; Generate a random operator.
Calculate the result.
Generate a number between 1 and 4 to indicate which part the user is to solve for.
Call a printQuestion function to display the problem depending on the partToSolveFor that prints the value or a ?.
Call a getAnswer function that depends on partToSolveFor.
Call a checkAnswer function that depends on firstOperand, operatorEnum, secondOperand, result, partToSolveFor and answer.
Then you write a test program that executes each individual function with a number of values and make sure each function does what it is supposed to.
When the individual functions are successfully tested, you then test the program as a whole..

Bivas B. answered 04/21/14
100% helpful and committed tutor working for your success.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Murshed A.
04/09/14