Thread: source of calculation error?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    source of calculation error?

    After compiling the source code and executing the .exe for debugging, I've found a calculation error when I try to convert celsius into fahrenheit, but I can't seem to figure out the source of it or how to fix it. Really appreciate your help, ppl.

    Code:
    //***************************************************************
    // Temperature Conversion Program
    // This program converts a Celsius temperature to its Fahrenheit
    // equivalent or vice versa
    //***************************************************************
    
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
    	float f;		// degrees farenheit
    	float c;		// degrees celsius
    	char option;		// type of conversion
    	char exit;		// flag for exit
    
    	cout.setf(ios::fixed, ios::floatfield);	// Set up floating
    	cout.setf(ios::showpoint);		// pt output format
    
    	do
    	{
    		// Introduction and Prompt type of conversion
    	
    		cout << "This program converts a Celsius temperature to"
    		     << endl << "its Fahrenheit equivalent or vice versa."
    		     << endl;
    		cout << "What type of conversion would you like to"
    		     << "perform? " << endl;
    		cout << "Enter \"c\" if you want degrees celsius" << endl;
    		cout << "and \"f\" if you want degrees farenheit. ";
    		cin >> option;
    
    		// Calculation and output
    
    		if (option == 'c')
    		{
    			cout << "Please enter the temperature in degrees";
    			cout << " farenheit: ";
    			cin >> f;
    
    			c = (5 / 9) * (f - 32);
    			
    			cout << setprecision(1) << f;
    			cout << " farenheits equals to " << setprecision(1);
    			cout << c << " celsius." << endl;
    		} // if (option == 'c')
    		else
    		{
    			cout << "Please enter the temperature in degrees";
    			cout << " celsius: ";
    			cin >> c;
    
    			f = (9 / 5) * c + 32;
    			
    			cout << f << " farenheits equals to " << c;
    			cout << " celsius." << endl;
    		} // else
    
    		// Prompt exit
    		
    		cout << endl;
    		cout << "Would you like to exit the program?('y' for yes) ";
    		cin >> exit;
    
    	} // do
    	while ( exit != 'y');
    	
    	system("pause");
    
    	return 0;
    
    } // int main()

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    c = (5.0 / 9.0) * (f - 32);
    f = (9.0 / 5.0) * c + 32;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    c = (5.0f / 9.0f) * (f - 32.0f);
    f = (9.0f / 5.0f) * c + 32.0f;

    You need to use floats when dividing. If you leave off the .0, it treats them as integers, and rounds it down.

    [edit]Beaten
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You need to use floats when dividing. If you leave off the .0, it treats them as integers, and rounds it down.
    More correctly it truncates not rounds

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >More correctly it truncates not rounds
    I don't get it. Why is saying it truncates more correct than saying it rounds?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because 2.9 truncates to 2, but rounds to 3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    another example:
    -4.6 would round to -5 but truncates to -4. Thus rounding down doesn't work in all cases.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thank you all for your help!
    I guess I haven't been very careful all along when I was writing my source code or scanning the code for debugging purposes.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, now that makes sense, thank you.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have another problem.
    I don't understand something about the conditions of the terms inside pow().
    This is one of the examples where compiler gives me an error message because of a problem with pow().

    Code:
    //***********************************************************
    // Lease payment program
    // This program calculates the remaining balance after
    // each payment
    //***********************************************************
    
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    
    const float PAYMENT = 165.25;	// amount of monthly payment
    const float I = 0.09;		// annual interest rate
    const int N = 36;		// total number of payments to be made
    
    int main()
    {
    	float balance;		// Remaining balance
    	float k;		// payment number
    
    	for ( k = 0; k <= 2; k++)
    	{
    		cout << "Payment #" << k << endl;
    		balance = PAYMENT * (1.0 - pow((1.0 + I), (k - N)) / I;
    		cout << "Balance remaining: $" << setprecision(2)
    		     << balance << endl;
    	} // for (k = 0; k <= 2; k++)
    
    	system("pause");
    
    	return 0;
    } // int main
    Please provide me a clearer explanation of the pow() function.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pow() takes two parameters of type double. The first parameter is the base and the second parameter is the power to raise the base to. pow() returns a double as the result of the calculation.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    To solve the problem with the second code, should I double float every identifier that has to do with the power function (b/c it requires both of its parameters as double) and use a different flag variable in the for loop because a integer variable is required?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM