Feedback??

Hi all I've just finished this kind of calculator and I would like to hear a little feed back on it, I tried to use a little bit of all the stuff I've been researching in the last couple weeks. I would just like to know if I seem to be going in the right direction or if there is something i'm doing that is bad practice.

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

using namespace std;

double add(double a, double b)
{
  double c;
  c = a + b;
  return (c);
}
double sub(double a, double b)
{
    double c;
    c = a - b;
    return (c);
}
double mult(double a, double b)
{
    double c;
    c = a * b;
    return (c);
}
double div(double a, double b)
{
    double c;
    c = a / b;
    return (c);
}
void incselect()
{
    cout << "\aINVALID!";
}
int main ()
{
    string name;
    string common;
    double a;
    double b;
    char mchoice;
    char again;
    double c;
    int END;

    cout << "Welcome to Calculate V.1\n";
    cout << "Please enter your name: ";

    getline(cin, name);

    cout << "\nHello " << name << ".\n";
    cout << "Please enter your first number: ";
    getline(cin, common);
    stringstream(common) >> a;

    do{
        cout << "\nPlease enter your next number: ";
        cin >> b;

        cout << "\nWhat would you like to do with: " << a << ", " << b << "?\n\n";

        do{
            cout << "[A]dd [S]ubtract [M]ultiply [D]ivide\n";
            cin >> mchoice;

            if (mchoice=='a'|| mchoice=='A')
            {
                c = add(a, b);
                END=0;
            }
            else if(mchoice=='s' || mchoice=='S')
            {
                c = sub(a, b);
                END=0;
            }
            else if(mchoice=='m' || mchoice=='M')
            {
                c = mult(a, b);
                END=0;
            }
            else if(mchoice=='D' || mchoice=='d')
            {
                c = div(a, b);
                END=0;
            }
            else
            {
                incselect ();
                END = 1;
                cout << '\n';
            }
        }while(END==1);

        a = c;

        cout << "Your final number is: " << a << '\n';

        do{
            cout << "\nAre you done with: " << a << "?\n\n[Y]es or [N]o ";
            cin >> again;

            if(again == 'n' || again == 'N' || again == 'y' || again == 'Y')
            {
               break;
            }
            else
            {
                incselect ();
                again= 't';
                cout << '\n';
            }

        }while(again == 't');

    }while(again == 'n' || again == 'N');

    return 0;
}

Hmm, so far it looks like you're going on a good track. Out of curiosity, is this an assignment for a class or something you're doing on your own?

Edit: You can save yourself a bit of headache with the choice == 'lowercase' || choice == 'UPPERCASE' statements by including <cctype> and using either std::toupper(char) or std::tolower(char) to uppercase or lowercase a character, respectively.

-Albatross
Last edited on
This is just something i'm doing on my own

ok i'll give that a shot haven't seen anything like that yet thanks
You should probably put in a case to prevent dividing by zero and optionally another case in to prevent any operations that would overflow the calculator.

258927389572895732897592837598273598235789723587 * 775828398275 //Overflow
7 / 0 //Undefined.
Last edited on
Doubles don't really have a problem with overflows, however. There is no real need to put in a set for that, because all that will happen is that you will print "inf" (infinity) instead of the value that you expected (e.g. 5.614e+613). The same goes for dividing by 0 with a double, you get "inf" as the result.

Here is an example:
http://ideone.com/TJhFBT
Last edited on
If you consider incorrect answers as being not problems, then there is no problem.

I also hate that / 0 doesn't give NaN.
What your calculator needs is a template, learn how to use C++ templates you won't regret it, they are extremely handy when it comes to calculating two 'unspecified' values and such.
Topic archived. No new replies allowed.