# Basic Calc

• 07-17-2002
Universe
Basic Calc
I am new here and I am new to C++... been slowly picking things up. I created a Basic Calc program and I was wonder what you guys think on how its writen. I know its a little extreme for a basic calc but I wanted to make it transferable and upgradable as much as possible.

Code:

```#include <iostream> class Math  //Math object that has all the basic math functions { public:   int Add() const { return numx + numy; }   int Sub() const { return numx - numy; }   int Div() const { return numx / numy; }   int Mult() const { return numx * numy; }   void SetXY(); private:   int numx;   int numy; }; void Math::SetXY()  //Prompts for X and Y {   cout << "\nx:";   cin >> numx;   cout << "y:";   cin >> numy; }; int main() {   using namespace std;  int menu();  int answer(int ans);  int select(unsigned short int sel);  unsigned short int sel = 0;  for (;sel != 5;)                                                        //Main Loop of Program   {     select(menu());  //Calling Menu function and passes it to the Select Function   }  return 0;                                                              //Exits Program } //Printing a menu on the screen and asking for a selection. int menu() {   unsigned short int select = 0;   for (;;)     {       cout << "1. Add\n";       cout << "2. Subtract\n";       cout << "3. Divide\n";       cout << "4. Mutiple\n";       cout << "5. Exit.\n";       cout << "\nSelect one:";       cin >> select;       if (select > 5)                  cout << "Invalid Selection, try again.\n";       else         break;     }   return select; } //Prints the Answer int answer(int ans) {   cout << "\nAnswer: " << ans << "\n"; } //Checking select to excute the correct function of object NewMath int select(unsigned short int sel) {   Math *NewMath = new Math;  //Creates math object on the heap   switch (sel)     {     case 5: cout << "Exiting...\n"; //Exiting Program       break;     case 4: cout << "Mutilping...\n";  //Calling Mutilpe Function       NewMath->SetXY();       answer(NewMath->Mult());       break;     case 3: cout << "Dividing...\n";  //Calling Divide Function       NewMath->SetXY();       answer(NewMath->Div());          break;     case 2: cout << "Subtracting...\n";  //Calling Subtract Function       NewMath->SetXY();       answer(NewMath->Sub());       break;     case 1: cout << "Adding...\n";  //Calling Add Function       NewMath->SetXY();       answer(NewMath->Add());     }   delete NewMath; //Delete Math object }```
• 07-17-2002
Klinerr1
wow, a lot more advnaced then my first calc
• 07-17-2002
DISGUISED
well it's a good start but it could use some work. I can tell you that I would get some errors if I compiled this.

Code:

``` int main() {   using namespace std;```
You declare the namespace in main, but your using iostream objects before main.

Code:

``` int answer(int ans) {   cout << "\nAnswer: " << ans << "\n"; }```
should be void, your not returning anything

Code:

``` int select(unsigned short int sel) {   Math *NewMath = new Math;  //Creates math object on the heap   switch (sel)     {     case 5: cout << "Exiting...\n"; //Exiting Program       break;     case 4: cout << "Mutilping...\n";  //Calling Mutilpe Function       NewMath->SetXY();       answer(NewMath->Mult());       break;     case 3: cout << "Dividing...\n";  //Calling Divide Function       NewMath->SetXY();       answer(NewMath->Div());          break;     case 2: cout << "Subtracting...\n";  //Calling Subtract Function       NewMath->SetXY();       answer(NewMath->Sub());       break;     case 1: cout << "Adding...\n";  //Calling Add Function       NewMath->SetXY();       answer(NewMath->Add());     }   delete NewMath; //Delete Math object }```
Ditto on this function. You got a great start here. I find the structure of the program a little strange in some areas but that will improve as you gain experience. Keep up the good work.
• 07-17-2002
Universe
Well... I didn't get any compile errors when I compiled it.

Quote:

You declare the namespace in main, but your using iostream objects before main.

Quote:

should be void, your not returning anything
Im not returning anything but the answer is passed into this function. I do need to pass in the answer to get it to display, correct? Ans is not a global var and it isn't in my object.

Thanks for the input...
• 07-17-2002
DISGUISED
Code:

```#include <iostream> using namespace std; class Math  //Math object that has all the basic math functions { public:   int Add() const { return numx + numy; }   int Sub() const { return numx - numy; }   int Div() const { return numx / numy; }   int Mult() const { return numx * numy; }   void SetXY(); private:   int numx;   int numy; }; void Math::SetXY()  //Prompts for X and Y {   cout << "\nx:";   cin >> numx;   cout << "y:";   cin >> numy; }; //Printing a menu on the screen and asking for a selection. int menu() {   int select = 0;   cout << "1. Add\n";   cout << "2. Subtract\n";   cout << "3. Divide\n";   cout << "4. Mutiple\n";   cout << "5. Exit.\n";   cout << "\nSelect one:";   cin >> select;   return select; } void answer(int ans) {   cout << "\nAnswer: " << ans << "\n"; } int main() {   Math *NewMath = new Math;    int sel = 0;   sel = menu();   while(sel > 0 && sel < 5)   {         switch (sel)     {     case 4: cout << "Mutilping...\n";  //Calling Mutilpe Function       NewMath->SetXY();       answer(NewMath->Mult());       break;     case 3: cout << "Dividing...\n";  //Calling Divide Function       NewMath->SetXY();       answer(NewMath->Div());          break;     case 2: cout << "Subtracting...\n";  //Calling Subtract Function       NewMath->SetXY();       answer(NewMath->Sub());       break;     case 1: cout << "Adding...\n";  //Calling Add Function       NewMath->SetXY();       answer(NewMath->Add());     }     sel = menu();   }   delete NewMath; //Delete Math object   cout << "Exiting...\n"; //Exiting Program   return 0;      //Exits Program }```
I don't have a compiler on this computer (and I have a headache) so this might not work but this is a little better structured and might show you some things you can do differently. Just play around with this and see if it helps you any and OH YEAH!! Welcome to the boards!
• 07-17-2002
Traveller
What DISGUISED is saying is that the return type on your function
Code:

```int answer(int ans) {   cout << "\nAnswer: " << ans << "\n"; }```
should be void since your not returning anything. However my practice is to just return 0, that way you know what your function is returning, and your not just leaving it up in the air. Just the way I do things however.
• 07-17-2002
Universe
Thanks, it does look cleaner and more understandable.

oh and BTW... it compiled perfectly.

Traveller:

I see it now.. it should be

Code:

```void answer(int ans) {   cout << "\nAnswer: " << ans << "\n"; }```
?
• 07-17-2002
DISGUISED
Quote:

so it returns something.
Right. And I do like his way better than void.
• 07-17-2002
Universe
Is there a reason why I should use

return 0

rather than

it gets the same thing accomplished.
• 07-17-2002
Traveller
by using int answer() and return 0 vice void with no specified return value you are leaving it up to the compiler to fill in a return value for you. This habit leaves things up to chance, and personally I believe it to be bad style. Their is nothing technically wrong with it, however programming is about 20% syntacs and 80% style.
• 07-17-2002
Kyoto Oshiro
You should also do "incorrect input" if the user enters below 0, instead of just above 5. For the main menu type thing...if you see what I mean...
• 07-17-2002
Sebastiani
The program might crash if the user tries to multiply or divide by zero.

Code:

``` int Div() const { return (!numx || !numy) ? 0 : numx / numy; } int Mult() const { return (!numx || !numy) ? 0 :  numx * numy; }```

In other words, just do some "psuedo" math on it if they do.
Anyway, you have a great style. Keep up the good work!
• 07-17-2002
civix
thats pretty good for a first calc...although if I were you, I would return another variable before the last } ....but that is just a suggestion :)