Hi, i've only been learning c++ for a couple days now, so im fairly new to it. :p. After I read the basic tutorials and got somewhat used to the c++ environment, I attempted to create a calculator and after trying to solve all of my errors, it became very tedious. I solved alot of the errors myself, which were minor mistakes, but I had no idea what was wrong when I got the error: LIBCD.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup
Debug/Calculator.exe : fatal error LNK1120: 1 unresolved externals. Any help would be highly appriciated! :D.

#include <iostream>

using namespace std;

int main()
{

    int x, y, z;
    char op;
    int cal(int x, int y, int& z, char op);

    cout << "Please enter your first number:  ";
    cin >> x;

        cout << "Please enter your second number:  ";
        cin >> y;

            cout << "Please enter the math operator that you would like to use (Eg. " << x << " ? " << y << ")";
            cin >> op;

        while ((op = '/') && (y == 0))
        {
            cout << "Error:  Cannot divide by zero." << endl;
            cout << "Please enter a legal math operator:  ";
            cin >> op;
        }

    cal(x, y, z, op);

                cout << x << op << y << " = " << z << endl;
                cin.get();

}

int cal(int x, int y, int& z, char op)
{

    if (op = '+')
    {
        z = x + y;
    }
        else
        {
            if (op = '-')
                z = x - y;
            else
            {
                if (op = '*')
                    z = x * y;
                else
                {
                    if (op = '/')
                        z = x / y;
                }
            }
        }
    

    return z;

}

Dani AI

Generated

The linker error you're seeing means the linker is looking for the Windows GUI entry point (WinMain) instead of the normal console main. As suggested, the simplest fix is to build a Console application. If you already created a Windows/GUI project change the project properties: Project → Properties → Configuration Properties → Linker → System → SubSystem and set it to Console (/SUBSYSTEM:CONSOLE). The other option (less common for a simple calculator) is to provide a WinMain function if you really want a GUI app.

There are also several code issues in the original post that will cause wrong behavior even after the linker problem is fixed. Use == for comparisons (for example if (op == '+')), not = which assigns. Don't declare the function prototype inside main—place the prototype above main or define the function before main. Make main return an int and end with return 0; as pointed out. Also watch integer division: if you want fractional results use double and always check the divisor for zero before dividing.

A simple, safer structure:

#include <iostream>

int calculate(int a, int b, char op) {
    switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return (b != 0) ? a / b : 0; // check before calling preferred
        default: return 0;
    }
}

int main() {
    int x, y; char op;
    if (!(std::cin >> x >> y >> op)) return 0;
    if (op == '/' && y == 0) { std::cerr << "Cannot divide by zero\n"; return 1; }
    std::cout << x << ' ' << op << ' ' << y << " = " << calculate(x, y, op) << '\n';
    return 0;
}

If the LNK2019 persists after switching to Console, do a full clean+rebuild and check Linker → Advanced → Entry Point (leave it blank to use the default). Stepping through with the debugger will quickly reveal logic errors like accidental assignments in conditions.

Recommended Answers

All 4 Replies

When you create the project, create a Console Application, not a GUI Application.

Hi ,

I compiled the program in VC and didnt got any error. Its working fine.
Only one warning. Thts beacuse u have declared main as
"int main (void)" and the main was not returning anything. Just add one "return 0" and the warning will vanish.

i have the same problem

commented: If you've the same problem: Just read this thread ok? But don't make such a dumb remark !! -1

i have the same problem

You have the same solution.
Are you sure that it's a good idea to reanimate 3-years old thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.