If, else statements

I am a student working on a project. It is a simple program but due to me missing some classes I am lost. The program is suppose to prompt the user for a type of problem +,-,*,/. Then it generates 2 random numbers to be used in the users selected arithmetic. The random number gen works but I don't know if i am setting the if statements up wrong or not.


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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
//******************Number Generator******************
int num1,num2;
int lseed;

lseed = 879377;
srand(lseed);
num1 = rand() % 50 + 1;
num2 = rand() % 9 + 1;
//***********************END**************************
string name;
int add, sub, multi, div, a1, a2, a3, a4; //integers for answers and input 
int a,s,m,d; //Choosing addition, subtraction, multiplication, division
//***********************
add = num1 + num2;
sub = num1 - num2;
multi = num1 * num2;
div = num1 / num2;;
//***********************
int input;
cout << "Please enter your name >> ";
cin >> name;
cout << "Glad to be working with you today " << name << ".  What type of problem do you want. ENTER + - / * : " << endl;
cin >> input;
//************************************************ADD
if (input == 'a')
   cout << "What is " << num1 << "+" << num2 << "? >> ";
   cin >> a1;
   if (a1 = add)
      cout << " You are correct!" << endl;
   else
       cout << " Better luck next time.- the correct answer is " << add << endl;
//************************************************SUBTRACT
else if (input == 's')
     cout << "What is " << num1 << "-" << num2 << "? >> ";
     cin >> a2;
     if (a2 = sub)
        cout << " You are correct!" << endl;
   else
       cout << " Better luck next time.- the correct answer is " << sub << endl;
//************************************************MULTIPLY
else if (input == 'm')
   cout << "What is " << num1 << "*" << num2 << "? >> ";
      if (a3 = multi)
      cout << " You are correct!" << endl;
   else
       cout << " Better luck next time.- the correct answer is " << multi << endl;
//************************************************DIVIDE   
else if (input == 'd')
   cout << "What is " << num1 << "/" << num2 << "? >> ";
   cin >> a4;
      if (a4 = div)
      cout << " You are correct!" << endl;
   else
       cout << " Better luck next time.- the correct answer is " << div << endl;


system("pause");
return(0);
}
Last edited on
input should be a char, and at lines 58, 50, 43, and 35 you have the wrong equals.

Also... your program will generate the same random numbers each time. Might I recommend:
1
2
#include <ctime>
srand((unsigned int)time(0));
?

Aside from that, keep at it! :)

-Albatross
Last edited on
Thanks man, really helped me out.
Topic archived. No new replies allowed.