Thread: this is the same program as before...solve is coming up crazy???

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

    this is the same program as before...solve is coming up crazy???

    I cannot figure this out...when i cout<<solve; it comes up some crazy number...like it has been assigned no value ever???? I don't know what I'm doing wrong?????
    Here is my program:

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

    float adding(a,b){return float(a+b);};
    float subtract(a,b){return float(a-b);};
    float multiply(a,b){return float(a*b);};
    float divided(a,b){return float(a/b);};
    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 '+': { 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;
    solve=0;}}
    if((sign=='+')||(sign=='-')||(sign=='*')||(sign=='/')){
    cout.setf(ios::showpoint);
    cout.setf(ios::fixed);
    cout<<"Your expression, ";
    cout<<setprecision(2)<<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;}


    Somebody Please Tell me how to do this...My teacher is an idiot and has no idea either....I need some help!!!!!

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    After modifying the code to get rid of the compiler errors, I entered "5 + 4" and it gave me the somewhat unsuprising result 9.00.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    well, Adrian, Please tell me how to change my code to clear compiler errors..I'm not that stupid..I know the math is right, but I'm just learning and I can't figure out how to fix the problem..the program runs but it's just not answering "solve" right

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The code above does not compile with Visual C++ 6.0 and I'm suprised it compiles with anything else.

    In these functions, a and b are undeclared...

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

    ... I changed them to...

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

    ... it then compiled okay and with a few test values, gave me the expected results.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    Thank you so much Adrian...It worked..I must have tried everything but that cuz it looks really familiar with all the stuff i've tried...Anyway, you've made my day..thank you

    ***And it compiled on Borland C++ builder 4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. can u solve this program :P
    By bawen in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2006, 12:16 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM