I gotta do this for class and can't figure it out!!! Help!!

This is a discussion on I gotta do this for class and can't figure it out!!! Help!! within the C++ Programming forums, part of the General Programming Boards category; I have got to use functions to do the calculations...my problem is I am trying to send solve back to ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Question I gotta do this for class and can't figure it out!!! Help!!

    I have got to use functions to do the calculations...my problem is I am trying to send solve back to the main ()...I've worked on this for all day...here is the source code:


    //PROBLEM: math choice
    #include <iostream.h>
    #include <conio.h>
    #include <iomanip.h>

    void multiply();
    void divided();
    void adding();
    void subtract();
    int main(){
    float a,b,solve;
    char sign, answer;
    do{
    cout<<"Please enter your mathematical"<<endl;
    cout<<"expression: ";
    cin>>a;
    cin>>sign;
    cin>>b;
    switch (sign){
    case '+': { adding();
    break;}
    case '-': { subtract();
    break;}
    case '*': { multiply();
    break;}
    case '/': { divided();
    break;}
    default : {cout<<"Please choose either +,-,*, or /."<<endl;}}
    if((sign=='+')||(sign=='-')||(sign=='*')||(sign=='/')){
    cout<<"Your expression, "<<a<<sign<<b<<" = "<<solve<<endl;}
    cout<<"Would you like to enter another expression?"<<endl;
    cout<<"Enter 'y' for yes and 'n' for no. Answer: ";
    cin>>answer;
    }while(answer=='y');
    getch();
    return 0;}

    //math evaluation functions
    void adding(a,b,solve){
    solve=a+b;}

    void subtract(a,b, solve){
    solve=a-b;}

    void multiply(a,b,solve){
    solve=a*b;}

    void divided(a,b,solve){
    solve=a/b;}


    ////// Here are my errors:
    [Linker Error] Unresolved external 'adding()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER4\PROJECTS\MATHCHOICE.OBJ.
    [Linker Error] Unresolved external 'subtract()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER4\PROJECTS\MATHCHOICE.OBJ.
    [Linker Error] Unresolved external 'multiply()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER4\PROJECTS\MATHCHOICE.OBJ.
    [Linker Error] Unresolved external 'divided()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER4\PROJECTS\MATHCHOICE.OBJ.

    /////SOMEBODY PLEASE HELP ME!!!!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    your prototypes need to match the function declarations here is what i would try

    double adding(double a, double b);
    or
    double adding(double,double);
    for the proto type



    double adding(double a, double b);
    {
    return (a+b);
    }

    for the function

    in main use
    solve=adding(a,b);

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    your function prototypes must match the functions definition.

    void multiply(); void divided(); void adding(); void subtract();
    defines 4 functions that take 0 arguements and do not return values. I would have the functions take 2 arguements(a, b) and return a float that is stored in solve.
    prototypes:
    float multiply(float, float);
    float divided(float, float);
    float adding(float, float);
    float subtract(float, float);

    definitions:
    float multiply(float a, float b){return a*b;}
    float divided(float a, float b){return a/b;}
    float adding(float a, float b){return a+b;}
    float subtract(float a, float b){return a-b;}

    your switch statement would look like
    switch(case)
    {
    case '+':
    solve = adding(a, b);
    break;
    case '-':
    solve = subtract(a, b);
    break;
    case '*':
    solve = multiply(a, b);
    break;
    case '/':
    solve = divided(a, b)
    break;
    default :
    cout<<"Please choose either +,-,*, or /."<<endl;
    }

    havent seen a switch statement with all the extra {}'s but it just may be a different style.
    suggestion: read about local variables, how variables are passes to functions, and functions with a return type

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    also

    If you are expecting multiple values take them as one line then parse them out....

    That way you have arguments that you can verify....
    zMan

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21