LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2003, 11:41 PM   #1
green_dragon37
Member
 
Registered: Oct 2002
Location: Lower Alabama
Distribution: Slackware, OpenBSD 3.9
Posts: 344

Rep: Reputation: 31
Need help with conversions in C


Hi all!

I've been messing around trying to learn C. I bought a book from O'Rielly called Practical C Programming. After a few chapters, I ran into an excercise that suggests writing a program to convert Centigrade to Farenheit, so I said, "How about a program that converts both ways??" Some of it works fine, but when I try to do conversions, the F-to-C conversion always produces -32.000000, and the C-to-F always produces whatever I input + 32. Could anyone help me figure this out??

Here is what i have so far:
Code:
#include <stdio.h>

char type; /* Type of conversion */
float far; /* Temp in Degrees Farenheit */
float cel; /* Temp in Degreen Celcius */
float result; /* Result of calculation */
char line[100]; /* Input Line */

int main()
{
	while (1){
	printf("Enter Type of Conversion;\n ? for help, q to quit:");

	fgets(line, sizeof(line), stdin);
	sscanf(line, "%c", &type);
	if((type == 'q') || (type == 'Q'))
		break;
	switch(type) {
		case 'f':
			printf("Enter temperature in degrees Farenheit: ");
			fgets(line, sizeof(line), stdin);
			sscanf(line, "%f", &far);
			cel = (5 / 9) * (far - 32);
			printf("%f Degrees Celcius\n", cel);
			break;
		case 'c':
			printf("Enter temperature in degrees Celcius: ");
			fgets(line, sizeof(line), stdin);
			sscanf(line, "%f", &cel);
			far = ((9 / 5) * cel) + 32;
			printf("%f Degrees Farenheit\n", far);
			break;
		case '?':
			printf("Type f for farenheit-celcius conversion;\nType c for celcius-farenheit conversion;\nQ to quit;\n? for help\n");
		default:
			printf("Unrecognised Command:  Type ? for help.\n");
		}
	}
}
Any help will be appreciated

Ian

Last edited by green_dragon37; 06-07-2003 at 01:06 AM.
 
Old 06-07-2003, 01:03 AM   #2
green_dragon37
Member
 
Registered: Oct 2002
Location: Lower Alabama
Distribution: Slackware, OpenBSD 3.9
Posts: 344

Original Poster
Rep: Reputation: 31
Nevermind, I found my mistake. I had set up the variables as floating point, but on the lines performing the calculations, I used integers. So where I used
Code:
cel = (5 / 9) * (far + 32);
I should have used
Code:
cel = (5.0 / 9.0) * (far +32.0);
What a mistake, huh.

Ian

Last edited by green_dragon37; 06-07-2003 at 01:07 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Conversions KissDaFeetOfSean Programming 3 07-21-2005 02:17 AM
XViD to MPEG-2 conversions? r_jensen11 Linux - Software 9 04-19-2004 06:59 AM
IDN conversions markus1982 Programming 1 03-11-2004 12:38 PM
Bulk image format conversions with a shell script? infidel Linux - Software 4 11-16-2003 07:20 PM
Graphics conversions that_girl Linux - Newbie 1 04-05-2002 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration