wrong output...

This is a discussion on wrong output... within the C++ Programming forums, part of the General Programming Boards category; the purpose of the program is to add||subtract||multiply||divide fractions, everything works fine, but when it comes to the output. The ...

  1. #1
    _Need-Your_Help
    Guest

    Exclamation wrong output...

    the purpose of the program is to add||subtract||multiply||divide fractions, everything works fine, but when it comes to the output.
    The difference between the result of the operation and 1.00 (
    diff=(1.00-result) is not displayed in fraction format that i intended

    heres the code,

    Code:
    ____________________________________________________
    
    //----------------------------------------------------------------
    cout<<"\n\n\a______________________________________________\n\n";
    cout<<"\t\t"<<frac.oneFRAC<<"/"<<frac.twoFRAC<<" "<<oper<<" "<<frac.numTWO<<"/"<<frac.denomTWO<<" = ";
    						
    		if (result==three_quarter)
    		cout<<"3/4"<<endl;
    		else
    		if (result==one_half)
    		cout<<" 1/2"<<endl;
    		else
    		if (result==one_quarter)
    		cout<<"1/4"<<endl;
    		else 
    		if (result==seven_eights)
    			cout<<"7/8"<<endl;
    		else 
    		if (result==five_eights)
    			cout<<"5/8"<<endl;
    		else 
    		if (result==three_eights)
    			cout<<"3/8"<<endl;
    		else 
    		if (result==one_eight)
    			cout<<"1/8"<<endl;
    		else 
    		if (result==one_sixteen)
    			cout<<"1/16"<<endl;
    		else
    		if (result==one_third)
    			cout<<"1/3"<<endl;
    		else 
    		if (result==two_third)
    			cout<<"2/3"<<endl;
    		else 
    		if(result==two_fifths)
    		cout<<"2/5"<<endl;
    		else
    		if(result==one_tenths)
    		cout<<"1/10"<<endl;
    		else
    		if(result==nine_tenths)
    		cout<<"9/10"<<endl;
    		else
    			cout<<setprecision(3)<<setiosflags(ios::fixed|ios::showpoint)<<result<<endl<<endl;
    
    //----------------------------------------
    		cout<<"            The difference from a whole (1.0) is:\n"<<endl;
    		cout<<"                 1.0 - "<<result<<" = ";
    		{
    		diff=1-result;
    		
    		if (diff==three_quarter)
    		cout<<"3/4"<<endl;
    		if (diff==one_half)
    		cout<<" 1/2"<<endl;
    		if (diff==one_quarter)
    		cout<<"1/4"<<endl;
    		if (diff==seven_eights)
    			cout<<"7/8"<<endl;
    		if (diff==five_eights)
    			cout<<"5/8"<<endl;
    		if (diff==three_eights)
    			cout<<"3/8"<<endl;
    		if (diff==one_eight)
    			cout<<"1/8"<<endl;
    		if (diff==one_sixteen)
    			cout<<"1/16"<<endl;
    		if (diff==one_third)
    			cout<<"1/3"<<endl;
    		if (diff==two_third)
    			cout<<"2/3"<<endl;
    		if(diff==two_fifths)
    			cout<<"2/5"<<endl;
    		if(diff==one_tenths)
    			cout<<"1/10"<<endl;
    		if(diff==nine_tenths)
    			cout<<"9/10"<<endl;
    		else
    			cout<<diff<<endl<<endl;
    			
    		}
    
    
    
    //OUTPUT::__________________________________________
    
                    1/2 + 2/5 = 0.900
    
                The difference from a whole (1.0) is:
    
                     1.0 - 0.900 = 0.100   <<<<<THIS SHOULD BE 1/10
    
                       ______________________________________
    
    Do you want to use the program again? (yes or no)
            >->:
    end of program
    __________________________________________________ __

    THANK YOU FOR YOUR HELP

    Fixed tags for you
    -Govtcheez

  2. #2
    Need-Your_Help_
    Guest
    the output in both the result and the difference has to be fractional...

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Obviously your three_quarter, one_half variables/defines have some meaning for you, but we need to know how you have defined them, show us where these get set. Also, if they are floating point values (float or double data type), then simply comparing result to these values using == might not work due to limits in precision. You may need to do something like this:

    Code:
    if ( (result >= three_quarter - FLT_EPSILON)
          && (result <= three_quarter + FLT_EPSILON) )
        cout<<"3/4"<<endl;
    Also, a final comment, if you really have these two long if-else if statements in your code, you might want to consider making it into a function that takes an argument. Then you just call the function twice, once for result and once for diff.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    -Need_Your HELP
    Guest
    then i should use double;

    the purpose of the program was to use the if statement, and struct

  5. #5
    -your help
    Guest
    the output should have the most common fractions like
    1/2, 3/4, but not like 1/234 or 2/549.

    the program will be inproved as I progress on the class that I am taking.

    heres values that i have for the variables

    [tag

    ---------------------------
    using namespace std;
    struct math

    {
    float oneFRAC, twoFRAC;
    float numTWO, denomTWO;
    };



    void main()
    {
    system("color 4B");
    string response;
    const float seven_eights=.875;
    const float five_eights=.625;
    const float three_eights=0.375;
    const float one_eight=0.125;
    const float one_sixteen=0.0625;
    const float three_quarter=0.750;
    const float one_half=0.500;
    const float one_quarter=0.250;
    const double two_fifths=0.400;
    const double one_tenths=0.100;
    const double one_third=0.33333;
    const double two_third=0.66666;
    const double nine_tenths=0.900;
    float Digit_1, Digit_2;
    char oper;
    float result;
    double diff;

    do{

    math frac; //define the structure object


    cout<<"\t\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ =+=+="<<endl;
    cout<<"\t\tEnter the nominator of the FIRST fraction\n"
    "\t\tThen press enter to input the denominator"<<endl;
    cout<<"\t\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ =+=+=\n\n"<<endl;


    cout<<" ";
    cin>>frac.oneFRAC; //input-(fract #1, num)
    cout<<"___";
    cout<<"\n";
    cout<<" ";
    cin>>frac.twoFRAC; //input-(fract#1, denom)
    system("cls");

    cout<<"\n What action do you want to perform?"<<endl;
    cout<<">> [ +, -, x, / ] <<"<<endl;
    cout<<" :";
    cin>>oper;

    system("cls");


    tag]

    thank you all for your help.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 06:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 12:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM

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