Using if/else to control program flow?

I am attempting to make a simple calculator. When I first build the addition part the program compiled and worked fine. but when I put the subtraction part in I got the following errors:
(expected primary-expression before "else")
(expected ";" before "else")
both errors are on line 43.
What I am wanting to do is get this part working, then I should be able to add the multiplication and division parts without incident. And at some point add a memory function and all the other bells and whistles.

Thanks!

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
// Calculator - Lets see if I can allow you to chose the operation and then
//            choose to do another operation after the first one.
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
char choice; 
 do
 {
// This block of code makes the first bit of text easier to read on screen.
   char operation;
   char a, s, m, d;
   cout << "Do you want to: \n" 
        << "A - Add\n"
        << "S - Subtract\n"
        << "M - Multiply\n"
        << "D - Divide\n"
        << endl;
   cin  >> operation;
   
 
 
 if (operation == a);  
 {double x, y, z;
   cout << "Enter the first number you want to Add: "
        << endl;
   cin  >> x;
   cout << "Now enter the seccond number: "
        << endl;
   cin  >> y;
   z=(x+y);
   
   cout << x
        << " + "
        << y
        << " = "
        << z
        << endl;
}
else if (operation == s)
{double d, e, f;
           cout << "Enter the fist number you want to subtract: "
                << endl;
           cin  >> d;
           cout << "Enter the seccond number you want to subtract: "
                << endl;
           cin  >> e;
           f =(d-e);
                
           cin  >> e;
           cout << d
                << " - "
                << e
                << " = "
                << f
                << endl;
}
    cout << "Do you want to continue? (y/n)"
         <<endl;
 cin >> choice;
}
while(choice != 'n');
}
closed account (z05DSL3A)
Line 26 if (operation == a); remove the ;
Ok, I removed the ";" from line 26 and the program compiled and did run.

However, when I enter "s" at the main menu the program goes to the addition instead of subtraction.
closed account (z05DSL3A)
You want to compare your operation variable with a Character literal, or initialize the variables a, s, m, and d.

So somthing like if (operation == 'a')
Thanks I was thinking the == would take that into account. I have the addition and subtraction working now. I had to remove line 53, don't know why I put that in there.

Thanks again.

Once I get it dont this way I think I will figure out how to do the same program using (switch).
Thanks for your help!
Here is the finished first draft:

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
100
// Calculator - Lets see if I can allow you to chose the operation and then
//            choose to do another operation after the first one.
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
char choice; 
 do
 {
// This block of code makes the first bit of text easier to read on screen.
   char operation;
   char a, s, m, d;
   cout << "Do you want to: \n" 
        << "A - Add\n"
        << "S - Subtract\n"
        << "M - Multiply\n"
        << "D - Divide\n"
        << endl;
   cin  >> operation;
   
 
 
 if (operation == 'a') 
 {double x, y, z;
   cout << "Enter the first number you want to Add: "
        << endl;
   cin  >> x;
   cout << "Now enter the seccond number: "
        << endl;
   cin  >> y;
   z=(x+y);
   
   cout << x
        << " + "
        << y
        << " = "
        << z
        << endl;
}
else if (operation == 's')
{
     double d, e, f;
           cout << "Enter the fist number you want to subtract: "
                << endl;
           cin  >> d;
           cout << "Enter the seccond number you want to subtract: "
                << endl;
           cin  >> e;
           f =(d-e);
                
           cout << d
                << " - "
                << e
                << " = "
                << f
                << endl;
}
else if (operation == 'm')
{
     double f, g, h;
     cout  << "What is the first number you want to multiply? "
           << endl;
     cin   >> f;
     cout  << "what is the seccond number you want to multiply? "
           << endl;
     cin   >> g;
     h = (f*g);
     cout  << f
           << " * "
           << g
           << " = "
           << h
           << endl;
}
else if (operation == 'd')
{
     double i, j, k;
     cout << "What is your Dividend? "
          << endl;
     cin  >> i;
     cout << "What is your Divisor? "
          << endl;
     cin  >> j;
     k = (i/j);
     cout << i
          << " / "
          << j
          << " = "
          << k
          <<endl;
}
    cout << "Do you want to continue? (y/n)"
         <<endl;
 cin >> choice;
}
while(choice != 'n');
}
Topic archived. No new replies allowed.