my calculator

Pages: 123
hi guys.i want to show you my best calculator i made so far. any tips for improvement.and also can u tell me how to let the user quit at the beginning by pressing q

ill post the code next cause i don't have enough room. i would like feed back whether it be bad or good. also can any of you who knows graphics and made a calculator with it can you pm me not the source code but the actual program...ill have to do the code in 2 replys cuz it is to big
Last edited on
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>

using namespace std;

class setup{
    public:

    void title(){
        system("TITLE calculator");
        system("color 2");
        cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
    }
};
    


int main()
{
    setup setup1;
    setup1.title();
    int howmany;
    int choice;
    double a;
    double b;
    double c;
    double d;
    double e;
    double average;
    char cchar;
    char cagain;
    
    cout << "type 1 to use a regular calculator.";
    cout << "type 2 to use a average calculator";
    cout << endl;
    cin >> choice;
    
    switch (choice){
           case 1:
    
    do{
    cout << "please enter the amount of numbers you want to use: ";
    cin >> howmany;
    
    switch (howmany){
    case 1:
        cout << "invalid number" << endl;
        break;
    case 2:

            cout << "please enter the first number you want to use" << endl;
            cin >> a;
            cout << "please enter the operation you would like to use" << endl;
            cin >> cchar;
            cout << "please enter the second number you would like to use" << endl;
            cin >> b;

            switch (cchar){
            
            case '+':
                cout << "the answer is: " << a << " + " << b << " = "
                << (a + b) << endl;
                break;
            case '-':
                cout << "the answer is: " << a << " - " << b << " = "
                << (a - b) << endl;
                break;
            case '*':
                cout << "the answer is: " << a << " * " << b << " = "
                << (a * b) << endl;
                break;
            case '/':
                cout << "the answer is: " << a << " / " << b << " = "
                << (a / b) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            cout << "would you like to start again (y or n)";
            cin >> cagain;
                break;
    
   
                     
                     
         
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 case 3:
            cout << "please enter the operation you would like to use" << endl;
            cin >> cchar;
            cout << "please enter the first number you would like to use" << endl;
            cin >> a;
            cout << "please enter the second number you would like to use" << endl;
            cin >> b;
            cout << "please enter the third number you would like to use" << endl;
            cin >> c;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << a << " + " << b << "+" << c << " = "
                << (a + b + c) << endl;
                break;
            case '-':
                cout << "the answer is: " << a << " - " << b << " - " << c << " = "
                << (a - b - c) << endl;
                break;
            case '*':
                cout << "the answer is: " << a << " * " << b << " * " << c << " = "
                << (a * b * c) << endl;
                break;
            case '/':
                cout << "the answer is: " << a << " / " << b << " / " << c << " = "
                << (a / b / c) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            cout << "would you like to start again (y or n)";
            cin >> cagain;
    case 4:
            cout << "please enter the operation you would like to use" << endl;
            cin >> cchar;
            cout << "please enter the first number you would like to use" << endl;
            cin >> a;
            cout << "please enter the second number you would like to use" << endl;
            cin >> b;
            cout << "please enter the third number you would like to use" << endl;
            cin >> c;
            cout << "please enter the forth number you would like to use" << endl;
            cin >> d;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << a << " + " << b << "+" << c << " + " << d << " = " 
                << (a + b + c + d) << endl;
                break;
            case '-':
                cout << "the answer is: " << a << " - " << b << " - " << c << " - " << d << " = "
                << (a - b - c - d) << endl;
                break;
            case '*':
                cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " = "
                << (a * b * c * d) << endl;
                break;
            case '/':
                cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " = "
                << (a / b / c / d) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            cout << "would you like to start again (y or n)";
            cin >> cagain;
    case 5:
            cout << "please enter the operation you would like to use" << endl;
            cin >> cchar;
            cout << "please enter the first number you would like to use" << endl;
            cin >> a;
            cout << "please enter the second number you would like to use" << endl;
            cin >> b;
            cout << "please enter the third number you would like to use" << endl;
            cin >> c;
            cout << "please enter the forth number you would like to use" << endl;
            cin >> d;
            cout << "please enter the fifth number you would like to use" << endl;
            cin >> e;

            switch (cchar){
            
            case '+':
                cout << "the answer is: " << a << " + " << b << " + " << c << " + " << d << " + " << e << " = "
                << (a + b + c + d + e) << endl;
                break;
            case '-':
                cout << "the answer is: " << a << " - " << b << " - " << c << " - " << d << " - " << e << " = "
                << (a - b - c - d - e) << endl;
                break;
            case '*':
                cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " * " << e << " = "
                << (a * b * c * d * e) << endl;
                break;
            case '/':
                cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " / " << e << " = "
                << (a / b / c / d / e) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            
        default:
            cout << "invlaid number.you can only use up to 5" << endl;
            }
    
            cout << "would you like to start again (y or n)";
            cin >> cagain;
    
    }while (cagain == 'y' || cagain == 'Y');
    break;
    
    case 2:
         do{
         cout << "enter how many numbers you want to find the average of (up to five)" << endl;
         cin >> howmany;
         
         switch (howmany){
                case 1:
                     cout << "you cant use that number it is too low" << endl;
                     break;
                case 2:
                     cout << "please enter the first number you want to use" << endl;
                     cin >> a;
                     cout << "please enter the second number you want to use" << endl;
                     cin >> b;
                     
                     average = (a + b) /2;
                     cout << "the average of those numbers is: " << average << endl;
                     break;
                case 3:
                     cout << "please enter the first number you want to use" << endl;
                     cin >> a;
                     cout << "please enter the second number you want to use" << endl;
                     cin >> b;
                     cout << "please enter the third number you want to use" << endl;
                     cin >> c;
                     
                     average = (a + b + c) /3;
                     cout << "the average of those numbers is:" << average << endl;
                     break;
                case 4:
                     cout << "please enter the first number you want to use" << endl;
                     cin >> a;
                     cout << "please enter the second number you want to use" << endl;
                     cin >> b;
                     cout << "please enter the third number you want to use" << endl;
                     cin >> c;
                     cout << "please enter the forth number you want to use" << endl;
                     cin >> d;
                     
                     average = (a + b + c + d) /4;
                     cout << "the average of those four numbers is: " << average << endl;
                     break;
                case 5:
                     cout << "please enter the first number you want to use" << endl;
                     cin >> a;
                     cout << "please enter the second number you want to use" << endl;
                     cin >> b;
                     cout << "please enter the third number you want to use" << endl;
                     cin >> c;
                     cout << "please enter the forth number you wamt to use" << endl;
                     cin >> d;
                     cout << "please enter the fifth number you want to use" << endl;
                     cin >> e;
                     
                     average = (a + b + c + d + e) /5;
                     cout << "the average of those five numbers is: " << average << endl;
                     break;
                     
                     }
    cout << "would you like to start again (y or n)";
    cin >> cagain;
    
    }while (cagain == 'y' || cagain == 'Y');
                     }
    system("pause");
    return 0;
}
www.pastebin.com

Tips for improvement? Make it so the user can use as many numbers as they would like, or better yet, parse an expression like:
(4 + 5) * 2 % (3.9 / 5)

Also, use some functions! That's a big friggin program!
Last edited on
First of all how would I let the user enter all the numbers he or she wants and where would I put in another function
You may want to try using vectors. Also I tend to stay away from the dreaded system () function.
Also as for your first question I am guessing you should try using the getline function and search the string for the +-*/% characters.
Last edited on
@moot1, It seems you are relatively new to C++. Your program is very inefficient, and relies on the fact that the user knows how to use your program specifically. While coding, you're supposed to assume that the user is really dumb, and that a 6yr old can use your program easily.
Also, who wants to simply add, multiply, subtract or divide all numbers!, also, for multiple input at once, haven't you heard of cascading?
ascii has a nice idea, but that involves strings. If you know how to use them, you can accept an expression from the user, and calculate the result, much like an actual calculator. And by functions, he means program functions. For repetitive statements, so that the overall length of your program is less. For a simple program like this, yours is damn long!!!
In your next post, just express your extent of knowledge in the C++ language, and I'll be able to help you out! :)

[This message may seem a little sharp, but it isn't meant to be! :)]
i couldnt really think of any way to shorten it or make it more user freinlly. can you please tell me a way to shorten it by like showing me a snippet of code because i didnt get fully what you meant above about functions.thanks
Firstly, in your switch cases, you use something like this:

1
2
3
4
5
6
7
case 3:
                     cout << "please enter the first number you want to use" << endl;
                     cin >> a;
                     cout << "please enter the second number you want to use" << endl;
                     cin >> b;
                     cout << "please enter the third number you want to use" << endl;
                     cin >> c;


Instead, what you could do is:
1
2
3
4
5
case 3:
{
         cout<<"\nPlease enter the three numbers: ";
         cin>>a>>b>>c;
}


This will shorten your code by almost 40-50 lines easily.

Nextly, you could write functions for the operations. This would be outside main, preferably above... Say:
1
2
3
4
float add(float a, float b)
{
         return (a+b);
}


So in any switch case, take the example of 5 numbers, you could do this:
 
cout<<"\nThe answer is: "<<((add(a, b))+(add(c, d))+e);


Or for that matter:
 
cout<<"\nThe answer is: "<<add(add(add((add(a, b), c), d), e);


This will again, make the program simpler, because now, you have four functions (one for each operation) doing all the work for you, and you no longer need to evaluate the result for each case. Just make a call to the function, and it'll do it for you. This makes the program readable, as well as memory efficient.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
using namespace std;
void title(){
        system("TITLE calculator");
        system("color 2");
        cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
    }
    int main()
{
    int howmany;
    int choice;
    double dnum1;
    double dnum2;
    double dnum3;
    double dnum4;
    double dnum5;
    double average;
    char cchar;
    char cagain;
    
    do{
    cout << "type 1 to use a regular calculator." << endl;
    cout << "type 2 to use a average calculator" << endl;
    cout << endl;
    cin >> choice;
    
    switch (choice){
           case 1:
    cout << "please enter the amount of numbers you want to calculate: ";
    cin >> howmany;
    
    switch (howmany){
    case 1:
        cout << "invalid number" << endl;
        break;
    case 2:
            cout << "Please enter the first number you want to use: ";
            cin >> dnum1;
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "Please enter the second number you would like to use: ";
            cin >> dnum2;
          switch (cchar){
             case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " = "
                << (dnum1 + dnum2) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " = "
                << (dnum1 - dnum2) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " = "
                << (dnum1 * dnum2) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " = "
                << (dnum1 / dnum2) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            cout << "Would you like to start again (y or n): ";
            cin >> cagain;
                break;
                }
    
    case 3:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter three numbers you want to use: ";
            cin >> dnum1 >> dnum2 >> dnum3;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << "+" << dnum3 << " = "
                << (dnum1 + dnum2 + dnum3) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " = "
                << (dnum1 - dnum2 - dnum3) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " = "
                << (dnum1 * dnum2 * dnum3) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " = "
                << (dnum1 / dnum2 / dnum3) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            }
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
            break;
    case 4:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter four numbers: ";
            cin >> dnum1 >> dnum2 >> dnum3 >> dnum4;

            switch (cchar){
        
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " + " << dnum3 << " + " << dnum4 << " = " 
                << (dnum1 + dnum2 + dnum3 + dnum4) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " - " << dnum4 << " = "
                << (dnum1 - dnum2 - dnum3 - dnum4) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " * " << dnum4 << " = "
                << (dnum1 * dnum2 * dnum3 * dnum4) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " / " << dnum4 << " = "
                << (dnum1 / dnum2 / dnum3 / dnum4) << endl;
                break;
            default:
                cout << "You cant use that operation";
                break;
            }
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
    case 5:
            cout << "Please enter the operation you would like to use: ";
            cin >> cchar;
            cout << "please enter five numbers: ";
            cin >> dnum1 >> dnum2 >> dnum3 >> dnum4 >> dnum5;

            switch (cchar){
            
            case '+':
                cout << "the answer is: " << dnum1 << " + " << dnum2 << " + " << dnum3 << " + " << dnum4 << " + " << dnum5 
                << " = " << (dnum1 + dnum2 + dnum3 + dnum4 + dnum5) << endl;
                break;
            case '-':
                cout << "the answer is: " << dnum1 << " - " << dnum2 << " - " << dnum3 << " - " << dnum4 << " - " << dnum5 
                << " = " << (dnum1 - dnum2 - dnum3 - dnum4 - dnum5) << endl;
                break;
            case '*':
                cout << "the answer is: " << dnum1 << " * " << dnum2 << " * " << dnum3 << " * " << dnum4 << " * " << dnum5 
                << " = " << (dnum1 * dnum2 * dnum3 * dnum4 * dnum5) << endl;
                break;
            case '/':
                cout << "the answer is: " << dnum1 << " / " << dnum2 << " / " << dnum3 << " / " << dnum4 << " / " << dnum5  
                << " = " << (dnum1 / dnum2 / dnum2 / dnum3 / dnum4) << endl;
                break;
            default:
                cout << "you cant use that operation";
                break;
            }
            
        default:
            cout << "Invlaid number.you can only use up to 5" << endl;
            
    
            cout << "Would you like to start again (y or n)";
            cin >> cagain;
            break;
            }
    break;
    case 2:
         do{
         cout << "Enter how many numbers you want to find the average of (up to five)" << endl;
         cin >> howmany;
         
         switch (howmany){
                case 1:
                     cout << "You cant use that number it is too low" << endl;
                     break;
                case 2:
                     cout << "Please enter the two numbers you want to use number you want to use: ";
                     
}
Last edited on
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
cin >> dnum1 >> dnum2;                   
                     
                     average = (dnum1 + dnum2) /2;
                     cout << "the average of those numbers is " << average << endl;
                     break;
                case 3:
                     cout << "Please enter three numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3;
                     average = (dnum1 + dnum2 + dnum3) /3;
                     cout << "the average of those numbers is " << average << endl;
                     break;
                case 4:
                     cout << "Please enter the four numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3 >> dnum4;
                     average = (dnum1 + dnum2 + dnum3 + dnum4) /4;
                     cout << "the average of those four numbers is " << average << endl;
                     break;
                case 5:
                     cout << "Please enter the five numbers you want to use: ";
                     cin >> dnum1 >> dnum2 >> dnum3 >> dnum4 >> dnum5;
                     
                     
                     average = (dnum1 + dnum2 + dnum3 + dnum4 + dnum5) /5;
                     cout << "the average of those five numbers is " << average << endl;
                     break;
                     }
    cout << "would you like to start again (y or n)";
    cin >> cagain;
    
    }while (cagain == 'y' || cagain == 'Y');
                     }
    system("pause");
    return 0;
is that better
Well, kinda. You haven't added any new functions, and for tasks like adding you could even just define a macro to do it like:
 
#define add(a, b) a + b 

So put some functions in there! And you still haven't added some kind of capability so that the user can enter as many numbers as they want, which can easily be done with a loop.

Also, I linked www.pastebin.com in my first message. Put your code there so you don't have to spew it out over a few posts.
Last edited on
See, just a simple cascading in your input has made your code 60 lines smaller. Imagine what effect functions will have!!!
@ ascii, yeah, macros are cool too, but they aren't very memory efficient, in spite of being speedy coz they are expanded everywhere! Whereas functions may be a fraction of a second slower, but very very memory efficient.. I have nothing against macros, but I guess to each his own, I'd rather use functions... :)
can u please send me a snippet or two of this code using functions-thanks
You have a class and a function already in your code. Just write some more.

@caprico, yeah it's a fair argument. I just like macros since they're shorter, and most people have enough memory nowadays that I don't find to be a problem.
ok and i got code::blocks because everyone says devc++ is bad.why when i try to compile it nothing happens
DevC++ is outdated as far as the debugger and linker are concerned. Also, quite a few errors are over-looked, if I've read up correctly. I use simple TurboC++...
so can you anser my question...why wont my program on codeblocks not compile
?
Pages: 123