What is wrong with my calculator code?

Hi there, I've just started trying to learn C++. I wrote this bit of code that I thought would be fairly simple that would work as a simple calculator with +,-, * and / functions.

Unfortunately, my calculator always gives me the result of 0.
Could anyone take a look through my code and see where I might have gone wrong?


Here is my Header File:
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
  #include "stdafx.h"
#include <iostream>

int Answer;
char Symbol; // was previously 'int Symbol'
int FirstNumber;
int SecondNumber;

int ReadNumber()
{
	using namespace std;
	cout << "What is the number you would like to input? ";
		int x;
	cin >> x;
	return x;
}

void ReadFunction()
{
	using namespace std;
	cout << "What is the function you would like to input? ";
	cin >> Symbol;
}

void WriteAnswer()
{
	using namespace std;
	switch (Symbol) {
	case '+':
		{
		int Answer = FirstNumber+SecondNumber;
		break;
		}
	case '-':
		{
		int Answer = FirstNumber-SecondNumber;
		break;
		}
	case '*':
		{
		int Answer = FirstNumber*SecondNumber;
		break;
		}
	case '/':
		{
		int Answer = FirstNumber/SecondNumber;
		break;
		}
	}
	cout << Answer << endl;
}


Here is my 'main' file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Calculator 2.cpp : Defines the entry point for the console application.
//

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

int main()
{
	using namespace std;
	int FirstNumber = ReadNumber();
	int SecondNumber = ReadNumber();
	ReadFunction();
	WriteAnswer();
	cin.clear();
cin.ignore(255, '\n');
cin.get();
	return 0;
}


Any other tips for basic formatting/conventions within coding that I have avoided would be very useful too.

Thanks!

Edit:
I have changed 'int Symbol' to 'char Symbol'
The problem is still present.

My debug output shows this message 4 times:
Cannot find or open the PDB file
I'm not sure if this is related to the problem?
Last edited on
Your storing Symbol as an int, You need to store it as a char.
Thank you, I hadn't noticed that.

After changing that, the problem still persists.


The debug output repeats this 4 times: Cannot find or open the PDB file
Just adding in for interests here.

I'm more of an idiot! when running your app so I'll point out some tips.

Firstly you already declaring your firstnumber and secondnumber in the header file so I would suggest you remove the declaring in the main file.

Already done here in the header below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include <iostream>

int Answer;
int Symbol;
int FirstNumber;
int SecondNumber;

int ReadNumber()
{
	using namespace std;
	cout << "What is the number you would like to input? ";
		int x;
	cin >> x;
	return x;
}



Now remove the declaring in the main file and stop confusing the program which numbers to use.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	using namespace std;
	int FirstNumber = ReadNumber();          //<----------------------------------- remove int
	int SecondNumber = ReadNumber();     //<----------------------------------- remove int
	ReadFunction();
	WriteAnswer();
	cin.clear();
cin.ignore(255, '\n');
cin.get();
	return 0;
}



"the user will never know what to input unless they have and idea.
if the user is to input a symbol ask and give and example" My instructor use to say.
example.

(Not much informative on what your app does);
What is the number you would like to input?
What is the function you would like to input?

Change to something like.

Please input the first number
Please input the second number


Try Asking for the first number then the symbol then the last number like you would do in a normal calculator else do as below to make it more simple.

Try making a menu for the user to choose the option to perform. You can put the main program in a loop so a the user can keep using the calculator instead of having to re-open the app.


1. divide
2. multiply
3.... makes it simple for the user to input a digit rater than a symbol

and in your switch case you can just use the number to check which operation must run. then before the switch case closes add a clear screen function and re-display your menu.


Lastly remove stdafx inclusion in the main as its included in the header.☺☺ tips and help as you asked. would like to see your code once done
Topic archived. No new replies allowed.