Atempting my first calculator

Hi guys,

I just started today with C++, watched a couple tutorials and read up on some information on the internet. My problem is that I am trying to make it so that i can enter

1
+
6

in my clalculator but the line of code giving me the most problems is

if (Calculation == + )
result = num1 + num2;
else if (Calculation == - )
result = num1 - num2;
else if (Calculation == * )
result = num1 * num2;
else if (Calculation == / )
result = num1 / num2;

I know i have to somehow tell the computer that the pluss in (Calculation == + ) is not to add, but instead a symbol that the user enters. How do I define this.

Thankyou for your time

Sincerely

Animerockfreak
Last edited on
Try putting your operators in single quotes. If you would post your whole program it would make it easier for us to help you. Also, please use code tags. Press the <> button under format and paste your code in the tabs that appear.
if (Calculatorion == '+')

You're missing your single quotes, I'm assuming you're using a char to get this input? You should be.

EDIT:
I was gonna fix my terrible typo, but I found it funny so I decided to leave it. You get the picture either way
Last edited on

Thank you that fixed it.
Below is my code I'm working on I'm trying to learn by doing this by myself untill i can't possibly get any further. Then I might ask again.

Thank you again.

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
// First try.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;


int main() {

int Option;
double num1, num2, result;
char Calculation;

cout << "Enter your Calculation: ";

cin >> num1;
cin >> Calculation;
cin >> num2;

if (Calculation == '+' ) 
	result = num1 + num2;
else if (Calculation == '-' )
	result = num1 - num2;
else if (Calculation == '*' )
	result = num1 * num2;
else if (Calculation == '/' )
	result = num1 / num2;

cout << "=" << result ;




	return 0;
}
Do you have some plan in using your variable "Option"?

Also, this may be beyond you, not sure where you're at. But, you should look into input validation as well, if you plan on building onto this program.
I was going to use the variable to make a loop later instead of closing the programm after its done calculating

Also thanks for the tip.
Last edited on
Topic archived. No new replies allowed.