My temp converter calculates wrong

Joined
Oct 23, 2010
Messages
8
Hello all!

This is my first post, so I hope I don't screw this up.

I've completed my homework program (more of a Linux compiler introduction than anything), but for some reason, I can't get the stupid thing to calculate correctly. The basic idea is to convert from Celsius to Fahrenheit, but you must use a separate function for the calculation.

Here is what I have so far. All it does is add 32 to the Celsius input.

Code:
#include <iostream>
using namespace std;

int convert ( double );

int main()
{
	double c = 0; // declare & initialize c
	cout << "Please enter a temperature in degrees Celsius:  "; // asks user for input
	cin >> c; // user input assigned to variable c
	cout << "That converts to " << convert( c ) << " degrees Fahrenheit." << endl;
	system("PAUSE");
} // end function main

int convert( double celsius )
{
	double fahrenheit = 0; // declare & initialize degrees Fahrenheit
	fahrenheit = 9/5 * celsius + 32; // formula for converting Celsius to Fahrenheit
	return fahrenheit; // returns converted result
} // end function convert

Thanks for any help you can give.

'Goose
 
9/5 is 1. Change the 9 to 9.0, 5 to 5.0, or both to use floating-point division. Alternately, you could put '(double)' infront of 9 or 5 to explicitly use floating-point division.
 
Yep.. I think literals are defined like this with x being a numeric value

double = x.x
ex: 5.0

float = x.xf
ex: 5.0f

int = x or
ex: 5

So you're doing integer division, which truncates, I try to keep my literals consistent even if it really doesn't matter... helps remind myself what type I'm dealing with.
http://www.cplusplus.com/doc/tutorial/constants/
 
Because your return value from the function is also an int.

You want to return a float/double.
 
int convert ( double );
to
double convert ( double );

Like Blazestorm said. It's best to be consistent. If your going to use double, then you should keep everything double. Otherwise you need to change your variables to integers.

Edit:
Make sure that you check your output log for errors and warnings. This would work, but it does show as an error and it is much better to be consistent with your code.
 
Last edited:
int convert( double celsius ) { .. }

It's returning an int. Change that to double.

Edit:
Heh, the 3 of us responded at almost the same time.
 
Thanks, everyone, for your help. It's amazing how much help you get when you show some effort. I didn't want to be one of those noobs that says "Do my homework for me." I'll be back, I'm sure.

Thanks again!

'Goose
 
Yea, that is one annoying part C / C++... implicit casts while nice, end up biting you in the ass. So you waste forever trying to track down bugs because something was cast from an int to a float and back...

I recommend compiling with warnings turned up, as well as treating warnings as errors... It'll prevent you from doing implicit casting and help you improve your coding practices. If you can't compile with warnings you a.) have to force the compiler to ignore the issue (like say you want to use "deprecated" functions) or b.) do it the "right" way...

If you're using GNU compiler, here are some flags to use...

-Wall -Wextra -ansi -pedantic -Werror -Wconversion

Code:
cc1plus: warnings being treated as errors
celcius.cpp: In function &#8216;int convert(double)&#8217;:
celcius.cpp:19: error: conversion to &#8216;int&#8217; from &#8216;double&#8217; may alter its value
 
Yea, that is one annoying part C / C++... implicit casts while nice, end up biting you in the ass. So you waste forever trying to track down bugs because something was cast from an int to a float and back...

I recommend compiling with warnings turned up, as well as treating warnings as errors... It'll prevent you from doing implicit casting and help you improve your coding practices. If you can't compile with warnings you a.) have to force the compiler to ignore the issue (like say you want to use "deprecated" functions) or b.) do it the "right" way...

If you're using GNU compiler, here are some flags to use...

-Wall -Wextra -ansi -pedantic -Werror -Wconversion

Code:
cc1plus: warnings being treated as errors
celcius.cpp: In function ‘int convert(double)’:
celcius.cpp:19: error: conversion to ‘int’ from ‘double’ may alter its value

I'd skip -ansi and -pedantic unless required, since GCC uses c90 instead of c99 (the current ANSI standard) when -ansi is specified.
 
Ah right, I also wasn't using C99... our CS classes required -ansi -pedantic for both G++ and GCC.

Along with compiling on Microsoft and Borland... which was fun dealing with all the little quirks they all have.
 
Last edited:
As a side thing: If you're compiling on a linux machine, clang has much nicer errors and warnings than gcc.
 
Back
Top