CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    7

    Question Celcius Conversion Loop

    So everything seems to be working fine here, but after the first calculation that converts 0C to 32F, it just adds increments of 1 to each Fahrenheit. Why isn't the formula being repeated for each increasing value of Celcius? Help would be greatly appreciated, as I have spent hours trying to figure this out.

    Code:
    # include <iostream>
    # include <iomanip>
    using namespace std;
    
    int main()
    {
    	
    	double c;
    	 cout << setw(4) << "Celcius" << setw(7) << "" << "Fahrenheit" << endl;
    	 
    
    	
    	for (c = 0; c <= 20; c++)
    		 {
    			double f = (9/5) * c + 32;
    			
    				
    
    			 cout << setw(4) << c << setw(7) << " " << " " << " " << " " << f << endl; 
    	}
    
    			  
    	 
    		 
    	return 0;
    	}

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Celcius Conversion Loop

    Quote Originally Posted by bertonius View Post
    So everything seems to be working fine here, but after the first calculation that converts 0C to 32F, it just adds increments of 1 to each Fahrenheit. Why isn't the formula being repeated for each increasing value of Celcius? Help would be greatly appreciated, as I have spent hours trying to figure this out.
    What do you think this gives you?
    Code:
    (9/5)
    When you divide an integer by an integer, the result is an integer.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2013
    Posts
    7

    Re: Celcius Conversion Loop

    Thank you, Paul. I changed the formula to (1.8 * c) + 32 instead, and it is working. Here's what it looks like now:
    Code:
    # include <iostream>
    # include <iomanip>
    using namespace std;
    
    int main()
    {
    	
    	int c;
    	 cout << setw(4) << "Celcius" << setw(7) << "" << "Fahrenheit" << endl;
    	 
    
    	
    	for (c = 0; c <= 20; c++)
    		 {
    			int f = (1.8 * c) + 32;
    			
    				
    
    			 cout << setw(4) << c << setw(7) << " " << " " << " " << " " << f << endl; 
    	}
    
    			  
    	 
    		 
    	return 0;
    	}

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Celcius Conversion Loop

    Quote Originally Posted by bertonius View Post
    Thank you, Paul. I changed the formula to (1.8 * c) + 32 instead, and it is working.
    You could have easily just done this:
    Code:
    (9.0 / 5.0)
    The numerator and denominator are both floating-point (double), so the result will be a double instead of having the truncation done as in (9/5).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured