Trying to make calculator type program

Jul 22, 2011 at 10:59pm
Can someone please tell me how to fix this? I'm trying to make a calculator that can divide, add, and subtract, but I get an error on line 16 I believe, saying "expected primary expression before ')' token". Here is the code:
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
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

         int main() {
             cout << "Hello" << endl;
             cout << "Please ender your desired calculation" << endl;
             int A;
             int B;
             int C;
             int D;
             cin >> A;
             cin >> B;
             cin >> C;
             if (B == -)
             {
             cout << A - C
             } 
             else if (B == +)
             {
             cout << A + C
             }
             else if (B == /)
             {
             cout << A / C
             }
             cin.ignore();
             cin.get();
             }

I thank you in advanced for any help. Oh and I'm kinda an idiot, so sorry if the answer is obvious
Last edited on Jul 22, 2011 at 11:01pm
Jul 22, 2011 at 11:10pm
A few points...

* B should be a char not an int.

* You need quotes around the operators in the program e.g.
 
if (B == '-') {

*Try to indent better.

*Use
 
int main(int argc, char **argv) {

*Don't use uppercase for general variable names.
Jul 22, 2011 at 11:36pm
I have another problem now. I can't figure out how to make the program stay open and let you keep putting in new calculations. Can someone tell me how?
Jul 22, 2011 at 11:42pm
Read about while loops.
Jul 23, 2011 at 12:19am
When I did a while loop, it made the program start then end instantly, doing the oppisite effect >.<
Jul 23, 2011 at 2:17am
I have ANOTHER problem. Man I'm an idiot.
Okay, I got the while loop, this is the code right now
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
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

         int main(int argc, char **argv) {
             cout << "Hello" << endl;
             cout << "Please ender your desired calculation" << endl;
             int A;
             char B;
             int C;
             int D;
             while (true)
             {
             cin >> A;
             cin >> B;
             cin >> C;
             if (B == '-')
             {
             cout << A - C;
             } 
             else if (B == '+')
             {
             cout << A + C;
             }
             else if (B == '/')
             {
             cout << A / C;
             }
             else if (B == '*')
             {
                  cout << A * C;
                  }
                  system("cls");
             cin.ignore();
             cin.get();
             
             }
             }


I can run it, I can do the first calculation, but when I do the second one, it glitches and displays the answer of the first calculation
Last edited on Jul 23, 2011 at 2:24am
Jul 23, 2011 at 5:58am
First of all, using arguments in main() is not necessary.

I have corrected the code.
The problem was lost with system("cls");

Heres the code(with proper indenting):

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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main() 
{
	int A;
	char B;
	int C;
	cout << "Hello" << endl;
	while (true)
    {		
		cout << endl << "Please ender your desired calculation" << endl;
		cin >> A;
		cin >> B;
		cin >> C;
        
		if (B == '-')
        {
			cout << A - C;
        }

        else if (B == '+')
        {
			cout << A + C;
        }

        else if (B == '/')
        {
			cout << A / C;
        }

        else if (B == '*')
        {
        cout << A * C;
        }

        cin.ignore();
        cin.get();
	}
}


EDIT:
Why did you create an integer D?
It was not used anywhere in the program. Just a waste of memory
Last edited on Jul 23, 2011 at 6:00am
Jul 23, 2011 at 10:12am
Integer D was meant for a feature of the program I hadn't yet added, it was supposed to be part of the code that let's you do one step algebra in the calculator, but I haven't decided exactly how I was going to code that out.
And can you please tell me what was wrong with system("cls")? is it because some people say system() stuff is evil? Haha.
Last edited on Jul 23, 2011 at 10:14am
Jul 23, 2011 at 10:14am
one step algebra

What do you mean by that?
Jul 23, 2011 at 10:23am
Like this: Say you input x = 2 + 8 it would tell you: x = 10
Jul 23, 2011 at 10:27am
Do you know about string parsing?
you could use that for it.

EDIT:
I had made a calculator sometime back as well, I had used a class for it
(though it could be made without it).
You can see the code here:
http://pastebin.com/F8hZUmEb

It takes the input in form of a single string like : 25*51
and will break it into three parts, 25, 51 and *
and then it functions the same as yours
Last edited on Jul 23, 2011 at 10:31am
Topic archived. No new replies allowed.