solved  C++ Automatic Calculator

crodino911 (7)Link to this post
Just try it and let me know what you think!

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
#include <cstdlib>
#include <iostream>
using namespace std;
float a,c,n;
char b,d;
float addizione(float a, float c){      //Function +
      float m=a+c;
      return(m);
      }
      float sottrazione(float a, float c){  //Function -
            float m=a-c;
            return(m);
            }
            float divisione(float a, float c){    //Function /
                  float m=a/c;
                  return(m);
                  }
                  float moltiplicazione(float a, float c){    //Function *
                        float m=a*c;
                        return(m);
                        }                 
int main()
{
          cin >> a;          // insert first integer
          LOOP:
          cin >> b;        // insert +-/*
          cin >> c;       // insert second integer 
          cin >> d;      // insert =
          cout << a;
          cout << b;
          cout << c;
          cout << d;
          if(d=='=' && b=='+'){
                    n=addizione(a,c);
                    cout << n;
                    a=n;
                    }else if(d=='=' && b=='-'){
                          n=sottrazione(a,c);
                          cout << n;
                          a=n;
                          }else if(d=='=' && b=='/'){
                                n=divisione(a,c);
                                cout << n;
                                a=n;
                                }else if(d=='=' && b=='*'){
                                      n=moltiplicazione(a,c);
                                      cout << n;
                                      a=n;
                                      }
                                      goto LOOP;   /*goes to loop and allows to 
                                                   automatically continue operating*/
          system("pause");
          return 0;
          }
          
Bazzy (2946)Link to this post
1
2
3
LOOP:
   //...
goto LOOP;
Using a while or a do-while is better

system("pause"); read http://www.cplusplus.com/forum/articles/7312/

1
2
3
4
5
6
7
8
9
cin >> a;          // insert first integer
LOOP:
cin >> b;        // insert +-/*
cin >> c;       // insert second integer
cin >> d;      // insert =
cout << a;
cout << b;
cout << c;
cout << d;
why is 'a' before the loop? And making the user type '=' is quite odd.
There is no input validation: http://www.cplusplus.com/forum/articles/6046/

1
2
3
4
float addizione(float a, float c){      //Function +
      float m=a+c;
      return(m);
      }
Can be written like this:
1
2
3
inline float addizione(float a, float c){      //Function +
      return a+c;
      }



Last edited on
crodino911 (7)Link to this post
Thank you for all your precious advices. 'a' is before LOOP: because you don't need to reinsert that value since in evrey function (if-else) n(the result of the operation)=a. So you can do
2
+
3
=
5 here you type the next operation for example * wich is 'b'
2 wich is c
=
10 wich is d.
and so on.
As you can see the result takes itself the place of 'a', that's why LOOP: is after 'a'.
Any other advice or consideration?

crodino911 (7)Link to this post
I just made some changes, i think it a lot more user-friendly now:
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
#include <cstdlib>
#include <iostream>
using namespace std;
float a,c,n,m;
char b,d;
inline float addizione(float a, float c){      //Function +
      return m=a+c;
      }
     inline float sottrazione(float a, float c){  //Function -
            return m=a-c;
            }
           inline float divisione(float a, float c){    //Function /
                  return m=a/c;
                  }
                 inline float moltiplicazione(float a, float c){    //Function *
                        return m=a*c;
                        }                 
int main()
{
          cin >> a;     // insert first integer
          LOOP:
          cin >> b;       // insert +-/*
          cin >> c;              // insert second integer     
          cout << a;
          cout << b;
          cout << c;
          if(b=='+'){
                    n=addizione(a,c);
                    cout << "=" << n << "; \n";
                    a=n;
                    }else if(b=='-'){
                          n=sottrazione(a,c);
                          cout << "=" << n << "; \n";
                          a=n;
                          }else if(b=='/'){
                                n=divisione(a,c);
                                cout << "=" << n << "; \n";
                                a=n;
                                }else if(b=='*'){
                                      n=moltiplicazione(a,c);
                                      cout << "=" << n << "; \n";
                                      a=n;
                                      }
                                      goto LOOP;   /*goes to loop and allows to 
                                                   automatically continue operating*/
          system("pause");
          return 0;
          }


I changed the structure of the function as you suggested me; there is no need to press '=' after every operation; you can type the whole calculation in one line and get a less "ugly" output like this:
2+4+5*2/2 (press enter and not '=')
2+4=6;
6+5=11;
11*2=22:
22/2=11;
Looks better?

kbw (1032)Link to this post
goto LOOP? Very naughty.

There an example of a calulator in the 1st edition of Stroustup's The C++ Programming Language that uses recursive decent.
kbw (1032)Link to this post
See chapter 6 in the current (3rd) edition.
Bazzy (2946)Link to this post
The code of that is here: http://www.research.att.com/~bs/dc_command_line.c
crodino911 (7)Link to this post
It is quite difficult for me to fully understand the whole code since i started programming in c++ from less than a week. However i don't understand what's the problem with using the goto statement(i apologize for the noob question).
Bazzy (2946)Link to this post
Read this: http://en.wikipedia.org/wiki/Goto#Criticism_of_goto_usage
crodino911 (7)Link to this post
Alright, i got it.However i'm trying to use a while or do-while instead of the goto statement but i just can't figure out how to do it whithout massive modification of the code.Any idea?
kbw (1032)Link to this post
The code is quite a leap to begin with, but if you have an interactive environment like Visual Studio or XCode, you can step thru the program one line at a time. You'll learn quite a lot from it.

The calculator allows to you make simple variable assignements (as in BASIC) and go on to use them in further operations. It's pretty cool.
crodino911 (7)Link to this post
Thank you, i'm looking at that right now and i find it really useful!I didn't know it existed even!By the way, what calculator are you talking about?Mine or the one posted in the link?
kbw (1032)Link to this post
Stroustup's version. The book will have some useful commentary.
Bazzy (2946)Link to this post
However i'm trying to use a while or do-while instead of the goto statement but i just can't figure out how to do it without massive modification of the code.

If you want to add a do-while loop in your code, you don't have to change much things:
replace LOOP: with do{
and goto LOOP; with }while(true);
Last edited on
crodino911 (7)Link to this post
Thank you very much, it was right in front of me and i just couldn't see it!

This topic is archived - New replies not allowed.