### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120`` `````` #include #include #include 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.