small problem in c++

Discussion in 'Programmer's Corner' started by mentaaal, Mar 6, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Hey guys,
    Annother wee problem in c++ again,
    I am writing a program to act as a simple calculator. Its basically an introduction to functions.

    This is the program:
    /* Write a program to simulate a basic calculator
    program has a function called menu() which displays choices to a user

    1) add two numbers
    2) subtract 2 numbers
    3) to multiply 2 numbers
    4) to divide 2 numbers
    5) to get the modulus of 2 numbers

    The functin menu() will return a value (1-5) back to main()
    Based on this number, use a switch case to call the appropriate function.
    Each function has 2 integer formal parameters and return a value.
    Pass this returned value and the 2 integer values to a display function to display result
    The 2 integer numbers are positive only and entered by the user */

    #include <iostream>
    using namespace std;

    int menu();
    int add(int,int);
    int subtract(int,int);
    int multiply (int,int);
    float divide (float,float);
    int mod(int,int);
    void displayfunction(int, int, int);

    int main()
    int option = menu();
    int value1,value2, result;

    switch (option)
    case 1:
    cout << "Please enter 2 values to add.\n";
    cin >> value1 >> value2;
    result = add(value1,value2);
    case 2:
    cout << "Please enter 2 values to subtract.\n";
    cin >> value1 >> value2;
    result = subtract(value1,value2);
    case 3:
    cout << "Please enter 2 values to multiply.\n";
    cin >> value1 >> value2;
    result = multiply(value1,value2);
    case 4:
    cout << "Please enter 2 values to Divide (2nd number is the divisor)\n";
    cin >> value1 >> value2;
    result = divide(value1,value2);
    case 5:
    cout << "Please enter a divide function to get the modulus of. (2nd number is the divisor)\n";
    cin >> value1 >> value2;
    result = mod(value1, value2);

    cout << "Invalid option";

    return 0;

    int menu()
    int option;
    cout << " Welcome to calculator, please choose from the following options:\n 1) Add\n 2) Subtract\n3)Multiply\n4)Divide\n5)Modulus\n Please choose by pressing any integer value between 1 and 5.\n";
    cin >> option;
    while ( option <1 || option >5 );
    return option;

    int add(int a, int b)
    return a+b;

    int subract(int a, int b)
    return a-b;

    int multiply(int a, int b)
    return a*b;

    float divide(float a, float b)
    return a/b;

    int mod(int a, int b)
    return a%b;

    void displayfunction(int a, int b, int c)
    cout << "The result of the calculation between: " << a << " and " << b << " is: \n" << c << endl;

    The problem i am getting is this:
    1>------ Build started: Project: Calculator, Configuration: Debug Win32 ------
    1>Calculator.obj : error LNK2019: unresolved external symbol "int __cdecl subtract(int,int)" (?subtract@@YAHHH@Z) referenced in function _main
    1>C:\Users\Greg\Documents\Visual Studio 2005\Projects\Calculator\Debug\Calculator.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Users\Greg\Documents\Visual Studio 2005\Projects\Calculator\Calculator\Debug\BuildLog.htm"
    1>Calculator - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    This is probably a simple thing but i just cant see to lay my eyes on the problem.
    Mark, as you will probably be the one to reply to this: i am aware that feeding in arguments of the wrong data type is a bad idea, but for the purposes of this question, my lecturer said it was ok.
    (Also we still havent covered type casting)

    Thanks for the help in advance,

  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    subract vs subtract

    Simple typo.
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    I concur.
    You declared subtract -- int subtract(int, int);
    You defined subract -- int subract(int a, int b) { return a - b; }
    You used subtract -- result = subtract(value1, value2);

    The linker was not able to link the code generated by the compiler for subract with the call to subtract.

    You're going to have other problems after you fix this one. According to the problem description, all of the functions should have two formal parameters of type int, yet one of yours does not. Furthermore, there is a type mismatch between the types of the values passed and the types of the formal parameters.
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Thanks n9352527 and mark!

    In the words of Achmed the terrorist, Ala dammit!