Need opinions

Hello so after sometime with C language like 8 days? i started with C++ this is my second day and i made this calculator :D it's working properly and the loop is good too. but i've seen similar calculator and they look very complicated O.O
please try my code and tell me how it is :D

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
  #include<iostream>
using namespace std;

int main()
{
	char opera;
	float a;
	float b;
	char again='Y';
	while(again=='Y'||again== 'y'){
	cout<<"first number"<<endl;
	cin>>a;
	
	cout<<"second number"<<endl;
	cin>>b;
	

	cout<<"enter yer operator "<<endl;
	cin>>opera;
	
	if(opera== '+'){
	
	
	  cout<<a+b<<endl;
}
	if(opera== '-'){
	
	
	
	  cout<<a-b<<endl;
}
    if(opera== '*'){
	
	
	
	  cout<<a*b<<endl;
}
	  
	  if(opera== '/'){
	
	
	
	  cout<<a/b<<endl;
	  
	  
	  
	  	 
}
 cout<<"wanna go again?. press Y or N"<<endl;
cin>>again;
}

}
This works fine for a simple operation with two numbers.

I think the reason why you mentioned having seen much more complicated programs for calculators is maybe because they can handle multiple numbers operations.

For instance, imagine that you have a calculator that doesn't ask you for the two numbers and the operator but rather lets you input the whole operation in such a fashion:
45 + 4 / 8

You would have to read this as a string, separate each number and operator, define priorities between operations (* and / go before + and -) and then calculate everything.

Complexity can scale up even more if you start taking brackets into account..
Last edited on
or make a nice reverse polish calculator. Then you just do what you are told and let the user handle order.
or make a nice reverse polish calculator.

What do you mean by that?
reverse polish is a different style.
lets see...
if you had cos(15^3+23) you would do it like this

15 commit

3 x to y (result, 15^3 committed)

23 add

result 15^3+23 committed

cos (result final answer committed)


this is usually done with a stack, you push values and when you get an operation, you pull off 2 values, perform the operation, and push the result back.

an algebraic calculator would use () buttons and several extra steps.

You can google reverse polish for a better explain … this was rather oversimplified.
Last edited on
Right I see, thanks for the info, I'll look it up, sounds rather interesting! :)
A "real" infix calculator with parentheses and stuff is a great exercise if you're inclined. Maybe in a few weeks: it'd be quite a challenge with only 8 days of experience.

I've got a bug report for you. Can you fix it?

Program output: end-of-input causes output spam


Environment:
Calculator program is named my-calc.exe on Linux 4.18.5, x86_64

Summary:
Running my-calc.exe with incomplete input generates output spam; htop(1) reports my-calc.exe single-core CPU usage at 100%. Ctrl-C can be used to terminate the program.

Steps to reproduce:
At a terminal:
1. Run ./my-calc.exe
program prompts "first number";
2. Type Ctrl-D

Expected behavior:
Program terminates

Actual behavior:
Program produces a constant repeating pattern of output, beginning with:
second number
enter yer operator 
wanna go again?. press Y or N
first number
second number
enter yer operator 
wanna go again?. press Y or N
first number

Repeating continually. Program no longer responds to input.
Last edited on
Topic archived. No new replies allowed.