problem with c++

This is a discussion on problem with c++ within the C++ Programming forums, part of the General Programming Boards category; please tell me what's wrong with the program thx. Code: #include<iostream> #include<cmath> #include<fstream> using namespace std; void menu(); void fuc1(); ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    problem with c++

    please tell me what's wrong with the program
    thx.

    Code:
    #include<iostream>
    #include<cmath>
    #include<fstream>
    using namespace std;
    
    void menu();
    void fuc1();
    void fuc2();
    double calculate(double);
    
    int main()
    {
        char choice;
        
        menu();
        cout<<"Enter choice: ";
        cin>>choice;
        
        if(choice=='+'||'-'||'*'||'/'||'%'||'P'||'A'||'X')
          fuc1();
        else if(choice=='R'||'F'||'T')
          fuc2();
        else
          exit;
        
        system("pause");
        return 0;
    }
    
    void menu()
    {
         cout<<"+,-,*,/,%"<<endl;
         cout<<"A\t averge"<<endl;
         cout<<"X\t maximum"<<endl;
         cout<<"P\t a^b"<<endl;
         cout<<"R\t reciprocal"<<endl;
         cout<<"F\t factorial"<<endl;
         cout<<"T\t square root"<<endl;
         cout<<"Q\t quit"<<endl;
    }
    void fuc1()
    {
         int operand1,operand2,result;
         char choice;
         
         cout<<"Enter two values you want to calculate: ";
         cin>>operand1 >>operand2;
         if(choice=='+')
           result=operand1+operand2;
         if(choice=='-')
           result=operand1-operand2;
         if(choice=='*')
           result=operand1*operand2;
         if(choice=='/')
           result=operand1/operand2;
         if(choice=='%')
           result=operand1%operand2;
         if(choice=='P')
           result=pow((double)operand1,(double)operand2);
         if(choice=='A')
           result=(operand1+operand2)/2;
         else
           if(operand1>operand2)
             result=operand1;
           else
             result=operand2;
         
         cout<<choice<<" "<<result<<" "<<operand1<<" "<<operand2<<endl;
    }
    void fuc2()
    {
         double operand1,result;
         char choice;
         
         cout<<"Enter one value you want to calculate: ";
         cin>>operand1;
         calculate(operand1);
         cout<<choice<<" "<<result<<" "<<operand1<<endl;
    }
    double calculate(double x)
    {
           char choice;
           double result;
           
           if(choice=='R')
             result=1/x;
           else if(choice=='F')
             for(int i=1;i<=x;i++)
                result*=i;
           else
             result=sqrt(x);
             
             return result;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    it did not print out the choice and the right answer

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if(x == 1 || 2)
    is not the same as
    Code:
    if(x == 1 || x == 2)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I tried it also, but still not working correctly

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by wind0 View Post
    I tried it also, but still not working correctly
    show the latest code and explain what do you mean by "not working correctly": what input you give, what output you get, what output you expect
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    when i chose '+', i entered 2 and 5, it should print out + 7 2 5.
    but what i got was 5 2 5, no + and wrong result

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Please post your corrected code.

    I tried it also, but still not working correctly
    Seems you have a trial on error point of view to it. When you program in C/C++ you should exactly know what you are doing. (Just trying to give a better point of view.)
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    void fuc1()
    {
         int operand1,operand2,result;
         char choice;  // choice declared here
         
         cout<<"Enter two values you want to calculate: ";
         cin>>operand1 >>operand2;
         if(choice=='+') // What is the value of choice here?
    See comments in code.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    i declared choice also in main, how should it be known in fuc?

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    OK this is what i did, thx to rags_to_riches
    I got the choice print, but the result is still incorrect.
    Code:
    #include<iostream>
    #include<cmath>
    #include<fstream>
    using namespace std;
    
    void menu();
    void fuc1(char);
    void fuc2(char);
    double calculate(char,double);
    
    int main()
    {
        char x;
        
        menu();
        cout<<"Enter choice: ";
        cin>>x;
        
        if(x=='+'||x=='-'||x=='*'||x=='/'||x=='%'||x=='P'||x=='A'||x=='X')
          fuc1(x);
        else if(x=='R'||x=='F'||x=='T')
          fuc2(x);
        else
          exit;
        
        system("pause");
        return 0;
    }
    
    void menu()
    {
         cout<<"+,-,*,/,%"<<endl;
         cout<<"A\t averge"<<endl;
         cout<<"X\t maximum"<<endl;
         cout<<"P\t a^b"<<endl;
         cout<<"R\t reciprocal"<<endl;
         cout<<"F\t factorial"<<endl;
         cout<<"T\t square root"<<endl;
         cout<<"Q\t quit"<<endl;
    }
    void fuc1(char choice)
    {
         int operand1,operand2,result;
         
         cout<<"Enter two values you want to calculate: ";
         cin>>operand1 >>operand2;
         if(choice=='+')
           result=operand1+operand2;
         if(choice=='-')
           result=operand1-operand2;
         if(choice=='*')
           result=operand1*operand2;
         if(choice=='/')
           result=operand1/operand2;
         if(choice=='%')
           result=operand1%operand2;
         if(choice=='P')
           result=pow((double)operand1,(double)operand2);
         if(choice=='A')
           result=(operand1+operand2)/2;
         else
           if(operand1>operand2)
             result=operand1;
           else
             result=operand2;
         
         cout<<choice<<" "<<result<<" "<<operand1<<" "<<operand2<<endl;
    }
    void fuc2(char choice)
    {
         double operand1,result;
         
         cout<<"Enter one value you want to calculate: ";
         cin>>operand1;
         calculate(choice,operand1);
         cout<<choice<<" "<<result<<" "<<operand1<<endl;
    }
    double calculate(char choice,double x)
    {
           double result;
           
           if(choice=='R')
             result=1/x;
           else if(choice=='F')
             for(int i=1;i<=x;i++)
                result*=i;
           else
             result=sqrt(x);
             
             return result;
    }

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
         if(choice=='+')
           result=operand1+operand2;
         if(choice=='-')
           result=operand1-operand2;
         if(choice=='*')
           result=operand1*operand2;
         if(choice=='/')
           result=operand1/operand2;
         if(choice=='%')
           result=operand1%operand2;
         if(choice=='P')
           result=pow((double)operand1,(double)operand2);
         if(choice=='A')   //Last if black
           result=(operand1+operand2)/2;
         else
           if(operand1>operand2)
             result=operand1;
           else
             result=operand2;
    It has a problem. Because you didn't use "else if" instead of a new if, all the conditions are checked even though we know only one will be true and when one is true all the others will be false. It also leads to another problem. The last "if" is always evaluated and in conditions that choice is not 'A' its else block is always executed. So your result will always be operand1 or operand2. Use switch statement instead of an if block to make the code easier to read.


    Code:
        
    else
          exit;
    This should be exit() to call the function (your compiler should already has generated a warning about this).
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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