C++ runtime errors, can someone help plz

Discussion in 'C Programming' started by BinWang85@hotmail.com, Oct 30, 2005.

  1. Guest

    The following is the code:

    #include <iostream.h>
    #include <math.h> //needed for decimal calculations


    int main()
    {
    int employee_num;
    double FetchHours(void);
    double FetchTotalSales(void);
    double CalcBasePay(double);
    double CalcCommisionPay(double);
    double CalcPayBeforeTax(double);
    double CalcTax(double);
    double CalcNetPay(double);
    char DispResults(void);
    char display;

    double W_sales, hours, sales_comm, base_pay;
    double pay_before_tax, tax_paid, net_pay; //assigned double for money
    (decimals)

    cout << "Please provide the following information \n";
    cout << "Employee number: \n";
    cin >> employee_num;
    if (employee_num < 0){
    cout << "Incorrect input for employee number\n"; //error check for
    negative number
    return(1);
    }

    hours = FetchHours();
    if (hours < 0) {
    cout << "Incorrect input for hours\n"; //error check for negative
    number
    return(1);
    }

    W_sales = FetchTotalSales();
    if (W_sales < 0) {
    cout << "Incorrect input for weekly sales\n"; //error check for
    negative number
    return(1);
    }

    base_pay = CalcBasePay(hours);

    sales_comm = CalcCommisionPay(W_sales);

    pay_before_tax = CalcPayBeforeTax(base_pay);

    tax_paid = CalcTax(pay_before_tax);

    net_pay = CalcNetPay(tax_paid);

    display = DispResults();

    return 0;
    }

    double FetchHours()
    {
    double hours;
    cout << "Hours worked per week: \n";
    cin >> hours;
    return hours;
    }

    double FetchTotalSales()
    {
    double W_sales;
    cout << "Weekly sales: \n";
    cin >> W_sales;
    return W_sales;
    }

    double CalcBasePay()
    {
    double hours;
    double base_pay;
    double base_rate = 23.00;
    base_pay=base_rate*hours;
    return base_pay;
    }

    double CalcCommisionPay()
    {
    double W_sales, sales_comm;
    if (W_sales <= 2500)
    sales_comm=W_sales*0.1500; //calcuate sales commission if weekly
    sales is
    //less than 2500 with 15% rate of weekly sales
    else if (W_sales > 2500)
    sales_comm=W_sales*0.25; //with 25% rate of weekly sales
    return sales_comm;
    }

    int CalcPayBeforeTax()
    {
    double pay_before_tax, sales_comm, base_pay;
    pay_before_tax=base_pay+sales_comm;
    return pay_before_tax;
    }

    int CalcTax()
    {
    double pay_before_tax, tax_paid;
    if (pay_before_tax > 3000)
    tax_paid=pay_before_tax*0.30; //calculate tax paid for pay before tax
    over 3000
    else if (pay_before_tax < 1000)
    tax_paid=pay_before_tax*0.10; //calculate tax paid for pay before tax
    under 1000
    else
    tax_paid=pay_before_tax*0.20; //calculate tax paid for any other
    amount of pay before tax
    return tax_paid;
    }

    int CalcNetPay()
    {
    double net_pay, pay_before_tax, tax_paid;
    net_pay=pay_before_tax-tax_paid;
    return net_pay;
    }

    void DispResults()
    {
    int employee_num;
    double base_pay;
    double sales_comm;
    double pay_before_tax;
    double tax_paid;
    double net_pay;

    cout << "Employee number: " <<employee_num << "\n";
    cout << "Base pay: $"<<base_pay<<"\n";
    cout << "Sales Commission: $"<<sales_comm<<"\n";
    cout << "Pay before tax: $"<<pay_before_tax<<"\n";
    cout << "Tax paid: $"<<tax_paid<<"\n";
    cout << "Net pay: $"<<net_pay<<"\n";
    }

    The errors I get are:

    Deleting intermediate files and output files for project 'Exercise8 -
    Win32 Debug'.
    --------------------Configuration: Exercise8 - Win32
    Debug--------------------
    Compiling...
    Exercise8.cpp
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(110) :
    warning C4244: 'return' : conversion from 'double' to 'int', possible
    loss of data
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(122) :
    warning C4244: 'return' : conversion from 'double' to 'int', possible
    loss of data
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(129) :
    warning C4244: 'return' : conversion from 'double' to 'int', possible
    loss of data
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(91) :
    warning C4700: local variable 'hours' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(98) :
    warning C4700: local variable 'W_sales' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(109) :
    warning C4700: local variable 'base_pay' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(109) :
    warning C4700: local variable 'sales_comm' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(116) :
    warning C4700: local variable 'pay_before_tax' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(128) :
    warning C4700: local variable 'pay_before_tax' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(128) :
    warning C4700: local variable 'tax_paid' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(141) :
    warning C4700: local variable 'employee_num' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(142) :
    warning C4700: local variable 'base_pay' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(143) :
    warning C4700: local variable 'sales_comm' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(144) :
    warning C4700: local variable 'pay_before_tax' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(145) :
    warning C4700: local variable 'tax_paid' used without having been
    initialized
    c:\bin\algonquin\programmingcst8110\exercise8\exercise8.cpp(146) :
    warning C4700: local variable 'net_pay' used without having been
    initialized
    Linking...
    Exercise8.obj : error LNK2001: unresolved external symbol "char __cdecl
    DispResults(void)" (?DispResults@@YADXZ)
    Exercise8.obj : error LNK2001: unresolved external symbol "double
    __cdecl CalcNetPay(double)" (?CalcNetPay@@YANN@Z)
    Exercise8.obj : error LNK2001: unresolved external symbol "double
    __cdecl CalcTax(double)" (?CalcTax@@YANN@Z)
    Exercise8.obj : error LNK2001: unresolved external symbol "double
    __cdecl CalcPayBeforeTax(double)" (?CalcPayBeforeTax@@YANN@Z)
    Exercise8.obj : error LNK2001: unresolved external symbol "double
    __cdecl CalcCommisionPay(double)" (?CalcCommisionPay@@YANN@Z)
    Exercise8.obj : error LNK2001: unresolved external symbol "double
    __cdecl CalcBasePay(double)" (?CalcBasePay@@YANN@Z)
    Debug/Exercise8.exe : fatal error LNK1120: 6 unresolved externals
    Error executing link.exe.

    Exercise8.exe - 7 error(s), 16 warning(s)

    Can someone suggest a solution to my errors, thanks
    , Oct 30, 2005
    #1
    1. Advertising

  2. Guest

    ....
    , Oct 30, 2005
    #2
    1. Advertising

  3. >The following is the code:
    >
    >#include <iostream.h>


    Since you think this is a C++ runtime error, try compiling with
    a C++ compiler and asking the question in a C++ group.

    Why are monetary amounts returned by 'int' functions, rather
    than double? Why do a lot of your functions have no arguments,
    then use uninitialized local variables to calculate something?

    Gordon L. Burditt
    Gordon Burditt, Oct 30, 2005
    #3
  4. Simon Biber Guest

    wrote:
    > The following is the code:
    >
    > #include <iostream.h>


    This is not C code. You should post to comp.lang.c++ instead.

    > #include <math.h> //needed for decimal calculations


    I can't think of any functions declared by <math.h> that do calculations
    in decimal. They mostly work with floating-point values, which are
    normally stored in a binary format.


    > int main()


    int main() is OK, but int main(void) is better.

    > {
    > int employee_num;
    > double FetchHours(void);
    > double FetchTotalSales(void);
    > double CalcBasePay(double);
    > double CalcCommisionPay(double);
    > double CalcPayBeforeTax(double);
    > double CalcTax(double);
    > double CalcNetPay(double);
    > char DispResults(void);


    Don't declare your functions inside another function. Function
    declarations should be at file scope, outside any function definition.

    If you order your functions correctly in the file, with the caller below
    the called, then you don't need to use a separate declaration at all.
    The prototype in the definition itself is enough.

    Note that the return type of DispResults here (char) does not match the
    return type of the function itself (void).

    > char display;
    >
    > double W_sales, hours, sales_comm, base_pay;
    > double pay_before_tax, tax_paid, net_pay; //assigned double for money
    > (decimals)


    Try to avoid using // comments when posting to newsgroups. They often
    get wrapped around, like in the line above, and then won't compile.

    > cout << "Please provide the following information \n";
    > cout << "Employee number: \n";
    > cin >> employee_num;


    Again, this is not C. Programs written in C generally use printf and
    scanf for this type of thing.

    > if (employee_num < 0){
    > cout << "Incorrect input for employee number\n"; //error check for
    > negative number
    > return(1);
    > }
    >
    > hours = FetchHours();
    > if (hours < 0) {
    > cout << "Incorrect input for hours\n"; //error check for negative
    > number
    > return(1);
    > }
    >
    > W_sales = FetchTotalSales();
    > if (W_sales < 0) {
    > cout << "Incorrect input for weekly sales\n"; //error check for
    > negative number
    > return(1);
    > }
    >
    > base_pay = CalcBasePay(hours);
    >
    > sales_comm = CalcCommisionPay(W_sales);
    >
    > pay_before_tax = CalcPayBeforeTax(base_pay);
    >
    > tax_paid = CalcTax(pay_before_tax);
    >
    > net_pay = CalcNetPay(tax_paid);
    >
    > display = DispResults();


    How does DispResults know what the values of base_pay, sales_comm, etc
    are? They are local variables.

    >
    > return 0;
    > }
    >
    > double FetchHours()
    > {
    > double hours;
    > cout << "Hours worked per week: \n";
    > cin >> hours;
    > return hours;
    > }
    >
    > double FetchTotalSales()
    > {
    > double W_sales;
    > cout << "Weekly sales: \n";
    > cin >> W_sales;
    > return W_sales;
    > }
    >
    > double CalcBasePay()
    > {
    > double hours;
    > double base_pay;
    > double base_rate = 23.00;
    > base_pay=base_rate*hours;
    > return base_pay;
    > }
    >
    > double CalcCommisionPay()
    > {
    > double W_sales, sales_comm;
    > if (W_sales <= 2500)
    > sales_comm=W_sales*0.1500; //calcuate sales commission if weekly
    > sales is
    > //less than 2500 with 15% rate of weekly sales
    > else if (W_sales > 2500)
    > sales_comm=W_sales*0.25; //with 25% rate of weekly sales
    > return sales_comm;
    > }
    >
    > int CalcPayBeforeTax()
    > {
    > double pay_before_tax, sales_comm, base_pay;
    > pay_before_tax=base_pay+sales_comm;
    > return pay_before_tax;
    > }
    >
    > int CalcTax()
    > {
    > double pay_before_tax, tax_paid;
    > if (pay_before_tax > 3000)
    > tax_paid=pay_before_tax*0.30; //calculate tax paid for pay before tax
    > over 3000
    > else if (pay_before_tax < 1000)
    > tax_paid=pay_before_tax*0.10; //calculate tax paid for pay before tax
    > under 1000
    > else
    > tax_paid=pay_before_tax*0.20; //calculate tax paid for any other
    > amount of pay before tax
    > return tax_paid;
    > }
    >
    > int CalcNetPay()
    > {
    > double net_pay, pay_before_tax, tax_paid;
    > net_pay=pay_before_tax-tax_paid;
    > return net_pay;
    > }
    >
    > void DispResults()
    > {
    > int employee_num;
    > double base_pay;
    > double sales_comm;
    > double pay_before_tax;
    > double tax_paid;
    > double net_pay;
    >
    > cout << "Employee number: " <<employee_num << "\n";
    > cout << "Base pay: $"<<base_pay<<"\n";
    > cout << "Sales Commission: $"<<sales_comm<<"\n";
    > cout << "Pay before tax: $"<<pay_before_tax<<"\n";
    > cout << "Tax paid: $"<<tax_paid<<"\n";
    > cout << "Net pay: $"<<net_pay<<"\n";


    No wonder your compiler complains that these variables were not
    initialised. They have been declared as separate local variables in this
    function, and are not connected to the ones declared in your main function.

    > }


    --
    Simon.
    Simon Biber, Oct 30, 2005
    #4
  5. wrote:
    > The following is the code:
    >
    > #include <iostream.h>


    You'll be wanting <news:comp.lang.c++>. C and C++ are two different
    languages.

    > #include <math.h> //needed for decimal calculations

    But the people in <news:comp.lang.c++> may prefer seeing the standard
    C++ header <cmath> instead of the C header <math.h> if only for
    namespace considerations.

    And they'd probably like seeing either a 'using namespace std;'
    statement or properly qualified names on lines like

    > cout << "Please provide the following information \n";
    Martin Ambuhl, Oct 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    882
    John C. Bollinger
    Mar 4, 2005
  2. Replies:
    1
    Views:
    353
  3. Replies:
    2
    Views:
    310
    Nick Keighley
    Nov 24, 2006
  4. Replies:
    1
    Views:
    321
  5. nocturnal
    Replies:
    1
    Views:
    498
    nocturnal
    Jul 10, 2009
Loading...

Share This Page