0
using namespace std;

//a class that will handle all the calculations requested by user

class MathOperations{
  public:
    void Message();
    int setSum(int,int);
    int setSuB(int,int);
    int setMul(int,int);
    float setDiv(float,float *);
    int setSqrt(int);
};

//Implementation:

void MathOperations:: Message(){
    cout<< " Welcome. This program simulates a calculator "<<endl;
}
// implementing the setters methods.
int MathOperations::setSum(int a, int b){

    int total;
    total = a + b;
    return total;
} 
int MathOperations:: setSuB(int a, int b){
    int total;
    total = a - b;
    return total;
}
int MathOperations:: setMul(int a, int b){
    int total;
    total = a * b;
    return total;
}

float MathOperations:: setDiv(float a, float *b){
    float result;
    if(b ==0){
        cout << "Using the Default Value of 1 because you can't devide by 0"<<endl;
    }
    else
        result = (a / *b);
    return result;
}

int MathOperations::setSqrt(int Square){
    int total;
    total = sqrt(Square);
    return total;
}

int main(int argc, const char * argv[])
{
    //creating instances of class MathOperations
    MathOperations add, sub, mul, div, sqrt;
    ///creating variable to hold user input

    int fnum;
    float snum;
    char opt= '0';

    //propt user for values
    cout << " Enter a Number"<<endl;
    cin >> fnum;

    cout << " Enter a second number " <<endl;
    cin >> snum;

    float total = div.setDiv(fnum, &snum);

    cout << total <<endl;


    cout << " What would you like to do '+','-','*','/' ?"<<endl;
    cin >> opt;

    switch (opt) {
      case '+' :
        {
            int total = add.setSum(fnum, snum);
            cout << " The Total Sum of both numbers is " << total <<endl;
        }
        break;
      case '-' :
        {
            int total = sub.setSuB(fnum, snum);
            cout << " The Subtraction of both Numbers is " << total << endl;
        }
        break;
      case '*' :
        {
            int total = mul.setMul(fnum, snum);
            cout << " The Multiplication of both Numbers is " << total << endl;
        }
        break;
      case '/' :
        {
            int total = div.setDiv(fnum, &snum);
            cout << " The Division of both Numbers is " << total <<endl;
        }
      default:
        cout << " Not a valid Option"<<endl;
    }

}

The division is not working properly. What am I doing wrong here? I'm trying to create a class with mathematical operations inside of it. I'm a beginner trying to do some practice here. Can you let me know what I'm doing wrong specifically?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
user1535963
  • 41
  • 2
  • 8
  • You should be rounding the result when you call `setDiv` (it might be something like 4.9999999, which is 4 as an `int`), and the pointer for the second parameter looks pretty pointless. – chris Jul 20 '12 at 16:22
  • 2
    You need to learn how to ask better questions: tell us what is wrong (what were you expecting to happen? What is happening instead?) and we'll tell you what you're doing wrong. – R. Martinho Fernandes Jul 20 '12 at 16:22
  • Why does `setDiv` have a pointer to float (not float) as an argument? – Bojan Komazec Jul 20 '12 at 16:22
  • Why are you using a float pointer instead of just a float? – jrad Jul 20 '12 at 16:23
  • @chris yeah i don;t want to use a pointer in fact i was just experimenting all that i want to accomplish is if the user try to devide by 0 use the default value which is one in the method.. and return back the result along with a cout indicating that dividing by 0 is not possible.. – user1535963 Jul 20 '12 at 16:24
  • Technically the result of floating point 0/0 is well-defined IIRC. Of course blocking it on a higher level makes sense. – chris Jul 20 '12 at 16:25
  • I agree with the guys above. Skip the pointer and tell us how it goes. Besides, why have a pointer only in one method? That should have been a clue since the setDiv() method is the only one not working :) – Gunnar Jul 20 '12 at 16:25
  • @ R. Marthinho i was just experimenting all that i want to accomplish is if the user try to devide by 0 use the default value which is one in the method.. and return back the result along with a cout indicating that dividing by 0 is not possible. – user1535963 Jul 20 '12 at 16:25
  • Fix this? You mean, the indentation? Simple `:se et|%!astyle -bj|ret`. Don't forget to save – sehe Jul 20 '12 at 16:33

3 Answers3

0

The second parameter of the function div should not be a pointer. At least I don't see any reason for it being a pointer.

So just remove the * and & from the variable snum.

petermlm
  • 930
  • 4
  • 12
  • 27
0

You're not protecting yourself from division by 0 in the setDiv function, because you're not comparing the second float to 0 first. You're only comparing its address to NULL.

And the error message doesn't make sense: the program doesn't "use 1" in any way.

And you're returning an uninitialised value if the pointer does happen to be NULL.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

When you say that you're using 1 instead of 0, you never change the value of b to 1.

jrad
  • 3,172
  • 3
  • 22
  • 24