Basic Calc

This is a discussion on Basic Calc within the C++ Programming forums, part of the General Programming Boards category; I am new here and I am new to C++... been slowly picking things up. I created a Basic Calc ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    10

    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
    }

  2. #2
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    wow, a lot more advnaced then my first calc

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    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.
    Last edited by DISGUISED; 07-17-2002 at 05:43 PM.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    10
    Well... I didn't get any compile errors when I compiled it.

    You declare the namespace in main, but your using iostream objects before main.
    I was thinking about this myself and I should have put that before my class.

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

  5. #5
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    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!

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    10
    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";
    }
    ?
    Last edited by Universe; 07-17-2002 at 06:12 PM.

  8. #8
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    so it returns something.
    Right. And I do like his way better than void.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    10
    Is there a reason why I should use

    int answer()
    return 0

    rather than

    void answer()

    it gets the same thing accomplished.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    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...
    "Um...well..."
    -Kyoto Oshiro

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    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!
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  13. #13
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21