Well just to start something rolling, two obvious issues I see is that on lines 36, 42, 48, 84, 90, 96, 132, 138, 144, you are using the '=' (assignment) operator to try to test for equality instead of the '==' (equality) operator.
e.g. apple == dog is checking for equality, but apple = dog is assigning apple the value of dog.
I'm also not sure what you are trying to do with your add, subtract, and multiply functionality. Your add, subtract, and multiply functions don't return anything; they are effectively useless in their current form. If you want to use the value, you should return it.
1 2 3 4 5 6
|
//add function
int add(int x, int y)
{
int answer=x+y;
return answer;
}
|
The way you set up your for loop is also very weird and error prone.
1 2 3 4 5 6 7
|
for (i=0; i<=3; i++)
{
for (i=1; i<=3; i++)
{
// ...
}
}
|
In your inner for loop, you are simply overwriting the value of i that's in the outer for loop. If you want two independent loop variables, you should give different variable names. But I don't think you need 2x for loops here -- one should be enough for your logic.
You should also consider keeping a variable inside only the smallest scope necessary for that variable. So, instead of defining int i at the beginning of your main, define it within the for loop itself.
1 2 3 4
|
for (int i = 0; i < 3; i++) // loop will run 3 times, not 4
{
// "i" is only now defined within the loop
}
|
You also have a lot of repetition in logic; I would suggest finding a way to factor out the similar logic into a function. But this isn't an error in itself.
___________________________________________________
___________________________________________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
//this is the program
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void display_numbers(int n1, int n2)
{
cout << "The first number is: ";
cout << n1 << endl;
cout << "The second number is: ";
cout << n2 << endl;
}
void ask_question(int n1, int n2, char operator_symbol)
{
cout << "What is " << n1 << " " << operator_symbol << " " << n2 << "? ";
}
int main()
{
srand(time(nullptr)); // initial random seed based on current system's time
int points = 0;
// ask the user what math operation they want to do:
int response;
cout << "Enter the number for the problem type desired:\n\t 1) Addition\n\t 2) Subtraction\n\t 3) Multiplication\n\n Enter Choice: ";
cin >> response;
int Number1 = rand()%100 + 1; // wheel 1 in range 1 to 100
int Number2 = rand()%100 + 1; // wheel 1 in range 1 to 100
display_numbers(Number1, Number2);
for (int i = 1; i <= 3; i++)
{
int correct_answer;
if (response == 1) // Addition
{
correct_answer = Number1 + Number2;
ask_question(Number1, Number2, '+');
}
else if (response == 2) // Subtraction
{
correct_answer = Number1 - Number2;
ask_question(Number1, Number2, '-');
}
else if (response == 3) // Multiplication
{
correct_answer = Number1 * Number2;
ask_question(Number1, Number2, '*');
}
else
{
cout << "ERROR! Invalid choice. Please re-run program" << endl;
return 1;
}
int user_answer;
cin >> user_answer;
if (user_answer == correct_answer)
{
cout << "CORRECT! \n";
if (i == 1)
{
points += 10;
}
else if (i == 2)
{
points += 5;
}
else if (i == 3)
{
points += 2;
}
break; // break out of loop early since user got it correct
}
else
{
cout << "WRONG! ";
if (i == 1)
{
cout << "TRY AGAIN.\n";
}
else if (i == 2)
{
cout << "LAST TRY.\n";
}
else if (i == 3)
{
cout << "GAME OVER!\n";
}
}
}
cout << "Your points: " << points << "/10" << endl;
}
|