Why is my multi-function calculator not working?

closed account (DiLwAqkS)
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
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int num1;
    int num2;
    int sum;
    int difference;
    int product;
    int quotient;
    char operation[3];


    cin >> num1,operation,num2;
   

    if (operation == "+"){
		sum = num1 + num2;
		cout << sum;
	}
	else 
		if (operation == "-"){
			difference = num1 - num2;
			cout << difference;
		}
		else
			if (operation == "*"){
				product = num1 * num2;
				cout << product;
			}
			else 
				if (operation == "/"){
					quotient = num1 / num2;
					cout << quotient;
					}
    
	cin.get();
	cin.get();
	return 0;
}


Sorry if it's something really obvious...I'm in high school and I only started teaching myself yesterday. The only potential error I could foresee would be an error in the if/else if statements. I identified all of the variables and everything. When I go to type in the statement (ex. 2+2), I press enter and it just goes to the next line in the command prompt and displays nothing. What am I doing wrong?
You have the input done, incorrectly. Try cin >> num1 >> operation >> num2;
cin >> num1,operation,num2;

is the same as (cin >> num1), operation, num2 ;

Look up the comma operator in your favorite reference.
cire, I can find where a value is returned from the group with comma separated expressions, such as Val = (Amt++, --Tot, Cnt+3);, but not while being used in data gathering, as above. Please point me to where you found this information. I found the program works with my answer, but not in the original post. Thanks.
If Val = (Amt++, --Tot, Cnt+3) were instead: Amt++, --Tot, Cnt+3
it would be the pretty much the same as the OP did. In this statement where the result of the comma operator isn't assigned to some variable, the code doesn't really have much effect. The result would be the same if it were written as:

1
2
3
Amt++;
--Tot ;
Cnt +3 ;


or the OP's code:

1
2
3
cin >> num1 ;
operation ;
num2 ;


The reason it parses like so is because of operator precedence. The comma operator has the lowest operator precedence, and the function call precedence is nearly the highest.
Last edited on
Sorry, cire. No matter what I try to do with it, that way just doesn't produce anything. I'll stick with my original answer, and continue doing it like that in my future programs. Thanks, though, for the attempted programming lesson. If it is possible to do it that way, why do I only see cin >> variable1 >> variable2 >> variable3 in others codes and in books?
Last edited on
Also you cannot use == to compare strings with a char array. Change 'operation' to be a std::string instead of a char array or just check the first character.
White... did you read what I wrote at all? The OPs code is the same as:

1
2
3
cin >> num1 ;
operation ;
num2 ;


which is nowhere near equivalent to:

cin >> variable1 >> variable2 >> variable3;
Yes cire, I read it, and tried it out in the OP's program, as he wrote. I do not get ANY results with it. But I do when I used the cin >>.., as I wrote in my answer. And as I previously asked,
Please point me to where you found this information.
, for I cannot see it being done your way in any of the books, or programs, I've seen.
Don't know if using MS Visual 2008 Express and CLR, has anything to do with the problem

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
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    int num1=0;
    int num2=0;
    int sum=0;
    int difference=0;
    int product=0;
    int quotient=0;
    char operation=' ';


    cin >> num1, operation, num2;
   

    if (operation == '+')
	{
		sum = num1 + num2;
		cout << sum;
	}
	if (operation == '-')
	{
			difference = num1 - num2;
			cout << difference;
	}
	if (operation == '*')
	{
				product = num1 * num2;
				cout << product;
	}
	if (operation == '/')
	{
					quotient = num1 / num2;
					cout << quotient;
	}

	cout << endl << "Num1 = " << num1 << " Operation = " << operation << " Num2 = " << num2 << ".." << endl << endl;
// num1 will equal first input, other two show nothing. 
    
	cin.get();
	cin.get();
	return 0;
}
// num1 will equal first input, other two show nothing.

is pretty much what:

1
2
3
cin >> num1 ;
operation ;
num2 ;


does. It's also the same as:

cin >> num1 ;

In other words, you're seeing exactly what you should, the last two expressions are what you might consider null operations. They do nothing.
Last edited on
@cire
But that is not what the program is supposed to do. You should be able to type, "5 * 7" <-enter, and get 35 as an answer. num1 gets the 5, operation gets the "*" and num2 gets the 7. The variable, operation, becomes the +, - * or /, as inputted. So, you see, the last two variables were NOT null operators
closed account (DiLwAqkS)
I haven't learned strings yet...they're further along in the tutorials I'm watching. What does checking a character mean? I switched
cin >> num1,operation,num2;
to
cin >> num1 >> operation >> num2;
and still no success...
Did you also change the double quotes around the +, -,etc, to single quotes. "+" is checking a string, '+' is checking a character. Operations is a char. That is how I have it, as shown in the above program I listed for you. That works as needed.
But that is not what the program is supposed to do.


Of course it's not. I was explaining why it didn't do what it was supposed to do. I wouldn't have had any need to post what I did if the OP's code had worked as he expected.

When you see a response below yours a very short time after yours was made (yours wasn't visible to me when I made mine,) assume it's addressed to the OP. Particularly if it doesn't address or contradict anything you said in your post.
closed account (DiLwAqkS)
thank you so much! it works!
Topic archived. No new replies allowed.