-3

I am making a simple calculator using function but i am having an error. I tried to resolved it could not do so. I tried again and again but i could not understand why i am having. I am using Dev C++ compiler. The statement in which i am having error else if(a-b)

#include<iostream>
using namespace std;
void fun(float a, float b); 
int main()
{   
    float a, b, sum, sub, mul, divide, mod;
    char op;

    //operands and operators are enterd by the user
    cout<<"Enter any two operands with operator=";
    cin>>a>>op>>b;
    fun(a, b);
    return 0;
}
void op(float a, float b)
{
    if(a+b)
    {
        float sum=a+b;
        cout<<"\nAddition of two numbers is="<<sum;
    }
    else if(a-b)
    {
        float sub=a-b;
        cout<<"\nSubtraction of two numbers is="<<sub;
    }
    else if(a*b)
    {
        float mul=a*b;
        cout<<"\nMultiplication of two numbers is="<<mul;
    }
    else if(a/b)
    {
        float divide=a/b;
        cout<<"\nDivision of two number is="<<divide;
    }
    else
    {
        cout<<"\nInvalid operator.......";
    }
}

Compiler errors:

calculator.cpp:(.text+0x68): undefined reference to `fun(float, float)`.
[Error] ld returned 1 exit status
JiaHao Xu
  • 2,452
  • 16
  • 31
Ijlal H
  • 69
  • 1
  • 8
  • 2
    Why isn't variable ```op``` used? – JiaHao Xu Dec 04 '18 at 04:20
  • 1
    Try to remove the fun function and replace the reference to it for the reference to op(float,float) – Bigbob556677 Dec 04 '18 at 04:20
  • 2
    You didn't define ```fun``` – JiaHao Xu Dec 04 '18 at 04:20
  • 1
    You declare and call the function `fun`, but you never define (implement) it. You seem to be missing some basic understanding in how C++ works, I recommend you get [a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). From the very start. – Some programmer dude Dec 04 '18 at 04:21
  • Sorry sir i could not understand what are talking about. I have used operator (op) – Ijlal H Dec 04 '18 at 04:21
  • 1
    All questions on stackoverflow.com must include all pertinent information ***in the question itself as plain text***. Dodgy links to shady external web sites can stop working at any time rendering the question meaningless. Images are not shown by text-only web browser. You need to [edit] your question and include all pertinent information, as plain text. If someone's asking for help, the least they could do is spend some effort to make it easy to read and understand the question. – Sam Varshavchik Dec 04 '18 at 04:21
  • @Ijlal Hussain operator overload does not work like this. Check [cppreference](en.cppreference.com) for further information in section ```language```. – JiaHao Xu Dec 04 '18 at 04:55

2 Answers2

2

You probably want to declare fun like that:

void fun(float a, char op, float b);

And define it using the same signature.

Then inside that function replace condition like if (a+b) by something like if (op == '+') and similar change for other operators.

For readability, you should put some spaces around operators. For example:

cin >> a >> op >> b;
float sum = a + b;

Finally, get rid of any unused variables. In main, you have sum, sub, mul, divide, mod.

Phil1970
  • 2,605
  • 2
  • 14
  • 15
2

Your code has 4 problems:

  1. First, you declared void fun(float, float) but did not define it. Instead, you define void op(float, float).

    Edit: operator overload in c++ does not work this way: check cppreference: operator overloading.

  2. The code inside void op(float, float) looks like nonsense to me. It doesn't serve the function of a calculator. You should use the variable op you defined in int main().

  3. using namespace std; is not a good habit.

  4. You didn't check the whether input/output succeeds or not.

So instead:

#include <iostream>

void calculate(float a, char op, float b);
int main()
{
    float a, b; /* These are useless: sum, sub, mul, divide, mod;*/
    char op; //operands and operators are entered by the user 

    std::cout.exceptions(std::cout.failbit);
    std::cin.exceptions(std::cin.badbit);
    std::cerr.exceptions(std::cerr.failbit);

    std::cout << "Enter any two operands with operator="; 

    if (std::cin >> a >> op >> b)
        calculate(a, op, b);

    return 0;
}

void calculate(float a, char op, float b)
{
    switch(op) {
        case '+':
            std::cout << "\nAddition of two numbers is " << (a + b);
            break;
        case '-':
            std::cout << "\nSubtraction of two numbers is " << (a - b);
            break;
        case '*':
            std::cout << "\nMultiplication of two numbers is= "<< (a * b);
            break;
        case '/':
            std::cout << "\nDivision of two numbers is= "<< (a / b);
            break;
        default:
            std::cerr << "\nUnkown operator!";
    }
}

You should really read some good tutorial books about C++ and read online documentations like cppreference to understand C++.

Community
  • 1
  • 1
JiaHao Xu
  • 2,452
  • 16
  • 31