GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-May-2006, 16:29
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Temperature Conversion Error


Hello, I have just started my Introduction to C programming course out at UCF, and I was wondering if some of you would be able to give some help.

I'll paste the basic assignment details:


Problem A: Temperature
This program will convert between Fahrenheit and Celsius. The information you will collect from the user is:
1. Magnitude of the temperature
2. Type of measurement
Prompt the user to input these values.

You will output a single statement showing both original and converted temperature values (as rounded integers).

Input Specification
1. Magnitude of the temperature is an integer and the program will read it into an integer variable (even though it would be possible to read it into a floating point variable, why am I enforcing this?).

2. Type of measurement is a single lowercase character, which will be used to determine the type of the temperature entered. For example, if the user enters “123 f”, this means 123 is in Fahrenheit and it needs to be converted to Celsius.

Output Sample
You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

Sample Run 1
Enter the temperature and type: 26 c
Converting from Celsius to Fahrenheit…
The formula is F=(9/5)*C+32
26 Celsius is 79 Fahrenheit.


Sample Run 2
Enter the temperature and type: 78 f
Converting from Fahrenheit to Celsius…
The formula is C=(5/9)*(F-32)
78 Fahrenheit is 26 Celsius.




I'll start off with the code:

CPP / C++ / C Code:
/*
	Assignment #1
	5/25/06
	Problem A: Temperature Conversion */
	
#include <stdio.h>

int main(void)
{
	float temperature;
	char symbol;
	
	//Ask the user to input the temperature and type.
	printf("Enter the temperature and type:\n");
	
	//User enters information.
	scanf("%f %c", &temperature, &symbol);
	
	//Check the user's input
	if ((symbol == 'f') || (symbol == 'c'))
		printf("Correct input, continuing..\n");
		else
		printf("Invalid temperature symbol.  Please enter f for fahrenheit, or c for celsius\n");
		
	//Calculate the user's correct input
		if (symbol == 'f')
			{
				printf("\n%s\n\n%s",
				"converting from Fahrenheit to Celsius..",
				"the forumula is C=(5/9)*(F-32)");
				printf("The temperature in Celsius is: %d.\n", (5/9) * (temperature - 32));
			}
				
		if (symbol == 'c')
			{
				printf("\n%s\n\n%s",
				"converting from Celsius to Fahrenheit..",
				"the forumula is F=(9/5)*(C+32)");
				printf("The temperature in Fahrenheit is: %d.\n", (9/5) * (temperature + 32));
			}
		return 0;
}



The problem that I'm having is with the equations. The rest of this program seems to complie, link, and run perfectly fine. However, when I enter the input to be converted, it always returns '0' for the answer.

I'm sure that the problem lies in my equations. If you find anything else that could be a problem, please let me know. This is my first time learning C and I'm really avid on learning the ins and outs of it all.

Thanks in advance! Looking forward to your replies!
  #2  
Old 27-May-2006, 16:51
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,017
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Basic C programming error


Since you take temperature as a 'float' type, use %f in your printf's instead of %d.

Or if you keep %d, you'll have to cast your printf expressions to 'int', i.e. (int)( your expression ).
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 27-May-2006, 17:34
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Re: Basic C programming error


EDIT: I got it working now. Thanks! I had my math equations down wrong. I just misplaced the parenthesis is all.

Thanks for the help on the decimal placement for the answer by the way.
  #4  
Old 27-May-2006, 17:53
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,017
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Basic C programming error


Good, I was going to ask about that calculation.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 27-May-2006, 23:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Basic C programming error


Hey Cecil, could you post your working code? I have a problem with what you posted that TurboPT didn't point out, and wondered if you fixed it.

Also, your formatting is a little horizontal heavy. Check out the Formatting code tutorial for different ideas.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 28-May-2006, 00:48
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Re: Basic C programming error


Certainly, here is the working code:
CPP / C++ / C Code:
/*	
	Assignment #1
	5/25/06
	Problem A: Temperature Conversion */
	
#include <stdio.h>

int main(void)
{
	float temperature;
	char symbol;
	
	//Ask the user to input the temperature and type.
	printf("Enter the temperature and type:\n");
	
	//User enters information.
	scanf("%f %c", &temperature, &symbol);
	
	//Check the user's input
	if ((symbol == 'f') || (symbol == 'c'))
		printf("Correct input, continuing..\n");
		else
		printf("Invalid temperature symbol.  Please enter f for fahrenheit, or c for celsius\n");
		
	//Calculate the user's correct input
		if (symbol == 'f')
			{
				printf("\n%s\n\n%s",
				"converting from Fahrenheit to Celsius..",
				"the forumula is C=(5/9)*(F-32)");
				printf("The temperature in Celsius is: %.f.\n", temperature = .5555555 * (temperature - 32));
			}
				
		if (symbol == 'c')
			{
				printf("\n%s\n\n%s",
				"converting from Celsius to Fahrenheit..",
				"the forumula is F=(9/5)*(C+32)");
				printf("The temperature in Fahrenheit is: %.f.\n", temperature = (1.8 * temperature) + 32);
			}
		return 0;
}


  #7  
Old 28-May-2006, 04:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Basic C programming error


OK, you fixed the problem I saw, but IMO you fixed it wrong.
CPP / C++ / C Code:
printf("\n%s\n\n%s",
"converting from Fahrenheit to Celsius..",
"the forumula is C=(5/9)*(F-32)");
printf("The temperature in Celsius is: %.f.\n", temperature = .5555555 * (temperature - 32));
#1) Yes, 5/9 is approx .5555555 but you really changed the equation. Not a big deal, but I feel it would be better keeping the equation intact:
CPP / C++ / C Code:
printf("The temperature in Celsius is: %.f.\n", 
      (5.0 / 9.0) * (temperature - 32));
When you had 5/9 you probably noticed the equation didn't work so you calculated the value of it. But if you simply add the .0 to the values the equation works.

#2) You shouldn't set the value of temperature in the printf()
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 28-May-2006, 18:42
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Re: Temperature Conversion Error


Ah, very nice observation. Thanks for the help!
  #9  
Old 28-May-2006, 21:09
amneziac85 amneziac85 is offline
New Member
 
Join Date: Jan 2004
Posts: 7
amneziac85 is on a distinguished road

Re: Temperature Conversion Error


this is interesting, ive seen similar issues.
__________________
Hosting Definitions http://www.***.com/gl/ Trust me, they'll help
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 11:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 21:30
What is "Ambigious symbol" ??*( a compilation error) small_ticket C++ Forum 2 07-Jan-2005 22:10
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 07:06
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 17:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.