C++ Calculator

Hi Everyone this is my Calculator it work almost fine now
but first i forgtten return 0 ;
en everytime i putted in a letter instead of a number it started to loop
now is my Question: What does the return 0: does?

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

using namespace std;

int main (void) {
	system ("Title Calculator by Svexo");
	char operation; // Operation mark
	double Fnum; // First number
	double Snum; // Second number
	
Calc: 
	
	cout << "Enter your Calculation: " ; 
	cin >> Fnum >> operation >> Snum; 
   
	
	switch (operation) {  l 
	case '+':
		cout <<"= " << Fnum + Snum << endl ; 
	
		break; 
	case '-':
		cout <<"= " << Fnum - Snum << endl;
		
		break;
	case '*':
		cout <<"= " << Fnum * Snum << endl;
		 
		break;
	case '/':
		cout <<"= " << Fnum / Snum << endl; 
		
		break;
	default:

		cout << "Error, Calculation not regonised"  << endl;
		
		break;
		/
	} 
	

return 0;  
}
Last edited on
What is the slash on line 39?
What's the point of the label named calc and the 1 at the end of line 17?
The return 0 informs the OS that called the prog that things ended normally. It's necessary to make sure your prog doesn't crash on exit.
When you input a letter it causes cin to act strangely for int variables you probably should flush the input buffer.
http://www.cplusplus.com/reference/iostream/istream/ignore/
Last edited on
RE :: tummychow

The 1 on line 17 and the slash on line 39 aren't suposed to stand there. Its a copy paste fault.

Why does my calculator loops when i press a letter instead of a number ?
Heres my 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream> 
 int CalculationErrors () ;
using namespace std;

int main (void) {
	system ("Title Calculator by Svexo");
	char operation; // Opertation mark
	double Fnum; // First number
	double Snum; // Second number
	
int temp = 0;
while (temp !=1){



	cout << "Enter your Calculation: " ; 
	cin >> Fnum >> operation >> Snum; 

   
	
	switch (operation) {  
	case '+': 
		cout <<"= " << Fnum + Snum << endl ; 
	
		break; 
	case '-':
		cout <<"= " << Fnum - Snum << endl;
		
		break;
	case '*':
		cout <<"= " << Fnum * Snum << endl;
		 
		break;
	case '/':
		cout <<"= " << Fnum / Snum << endl; 
		
		break;
	default:

		CalculationErrors () ; 

		break;

		 
		
	}	
	} 
		
   

}

int CalculationErrors (void) 
{
    cout << "Error, Wrong input" << endl; 
	main();
	 return 0;
}
Why do you use system() on line 6? I assume you mean printf() or cout<<. If I were you I'd use:
1
2
3
4
5
6
#include <stdio.h>
long double Fnum; // First number
long double Snum; // Second number
char buff[64];
scanf("%64s", buff);
sscanf(buff, "%Le%c%Le", &Fnum, &operation, &Snum);

Also, if you want your loop to be infinitive, just use while(1), but you better use something like while(! (Fnum==0 && Snum==0)).
try this..

1
2
3
4
5
6
int CalculationErrors (void) {
    cout << "Error, Wrong input" << endl;
    cin.clear();
    cin.ignore(256, '\n');
    return 0;
}


and do not call main();
Why do you use system() on line 6? I assume you mean printf() or cout<<. If I were you I'd use:

1
2
3
4
5
6
7
8
9
10
 #include <stdio.h>
long double Fnum; // First number
long double Snum; // Second number
char buff[64];
scanf("%64s", buff);
sscanf(buff, "%Le%c%Le", &Fnum, &operation, &Snum);
 


Also, if you want your loop to be infinitive, just use while(1), but you better use something like while(! (Fnum==0 && Snum==0)).



No it's to change the title from the Console window
did you try my code to stop the loop when you input a character?
Yes,, and its working ,, tnx :)

did you try my code to stop the loop when you input a character?

Topic archived. No new replies allowed.