Calculator

Hello

I am somewhat new to c++ so im not sure why this is happening this is what I have so far
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
#include "stdafx.h"


#include <iostream>
using namespace std;

int main()
{
	int a;
	int b;
	int Operater;
	int answer;
			
	cout << "Hello, This Application is a calculator first you must enter a number." << endl; 
	cout << "Next The app will ask of you want to add, subtract, Multiply or divide. "<< endl;
	cout << "Next you will enter the next number." << endl;
	cout << "Once you hit ENTER the application will calculate an answer. "<< endl;
	cout << "Lets Begin, Please enter a number"<< endl;

	cin >> a;
	
	cout << "Now type your operator 1=add 2=subtract 3=multiply 4=divide"<< endl;

	cin.get();
	return 0;
}


But once you type the number and hit enter the console closes Im not sure what to do to fix this and keep the console open.


Thanks in advance
Last edited on
Add this at the end.

getchar()

Of course, before return 0;
Last edited on
u could also include <conio.h> and use getch(); or _getch(); in visual studio.

my personal preference is system("PAUSE");

another way is also:
fflush(stdin);
puts("Enter to exit!");
getchar();
It's best to just run a console program from the console. On windows, navigate to the executable directory, shift+rightclick into the directory, click open cmd here (or something like that).

Or WIN+R -> cmd
> cd >>your directory<<

Then just type the name of the executable (you may drop the .exe extension). On Mac - I have no idea, but there should be something like bash available. On any linux distro, you should already know how to operate a shell.

The problem is, when a console program is done its done. It shouldn't block (because console programs are meant to run from the console). There are ways to keep the console open, but they all go against the very idea of a console program.
Last edited on
Fantastic Thanks for the help
my personal preference is system("PAUSE");

You shouldn't use any system commands unless you completely know what you are doing and know why.
ok now I seem to have a new problem the program does not seem to be calculating correctly
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
#include "stdafx.h"


#include <iostream>
using namespace std;

int main()
{
	int a;
	int b;
	int Operater;
	int answer;
			
	cout << "Hello, this application is a calculator first you mount enter a number." << endl;
	cout << "Next you will enter the next number." << endl;
	cout << "Next The app will ask of you want to add, subtract, Multiply or devide. "<< endl;
	cout << "Once you hit ENTER the application will calculate an answer. "<< endl;
	
	cout << "Lets begin, please enter a number"<< endl;
	cin >> a;
	
	cout << "Now chose your second number to use."<< endl;
	cin >> b;

	cout << "Now chose your operator 1=add 2=subtract 3=multiply 4=divide"<< endl;
	cin >> Operater;
		if (Operater = 1)
			answer = a + b;
		if (Operater = 2)
			answer = a - b;
		if (Operater = 3)
			answer = a * b;
		if (Operater = 4)
			answer = a / b;
		
		
					
	cout << "Your answer is ";
	cout << answer;
		
		
	cin.get();
	getchar();
	return 0;
} 
Last edited on
You need == in your if statements, not =. Might be better to use else if statements too. Or a switch, if you prefer.
Last edited on
I'm still kinda new to this kinda thing so I was wondering if someone could explain to me in the noobiest way possible how switch statements work and how it differs from an else statement.
Switch statements can be faster than if statements, for example:

1
2
3
4
5
switch (a + b){
case 1: cout << "1"; break;
case 2: cout << "2"; break;
deafult: cout << "Thats too high!";
}


is a little bit faster than
1
2
3
if (a + b == 1) cout << "1";
else if (a + b == 2) cout << "2";
else  cout << "Thats too high!";


In that case you only make the computation of what a + b is once. You might do something like:
1
2
3
c = a + b;
if (c == 1) ...
...


But now you have a variable c that you might not need again.

The if statements starting at line 27 are not the "fastest" though (keep in mind iHutch's correction). Here you run through 4 conditions no matter what. If the middle two started "else" and the last one was just "else" then the program wouldn't keep looking if it found a matching case.
Last edited on
Topic archived. No new replies allowed.