Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C program help


  • Please log in to reply

#1
bluntknife

bluntknife

    Member

  • Member
  • PipPip
  • 55 posts
hi, i have created a temp converter which will convert from fahrenhiet to celsius and vice versa. it is menu driven but for some reason it wont convert from celsius to fahreenheit but everything else works

any help would be a precaded

here is the code for the celsius to fahrenheit conversion;

printf("***************************************************************\n");
printf(" >> Celsius to Fahrenhiet converter <<\n\n");
printf(" >> Enter temperature in Celsius = ");
scanf("%d", &ce);
fa=(ce*9/5)+32;
printf(" >> The tempurature in Fahrenhiet is %d\n", ce);
printf("***************************************************************\n");
break;

just say if you need more info

many thanks
  • 0

Advertisements


#2
bobmad

bobmad

    Member

  • Member
  • PipPipPip
  • 344 posts
The answer to: x = 3 / 2 is 1 even if x is declared a float!!

(ce* 9 / 5 ) is not giving you what you are expecting...
  • 0

#3
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Here's code giving you what you want. I included plenty of comments so that you (hopefully) will understand it. If you don't understand something, please ask about it! If you just copy this, but don't understand it, then you're not learning anything and it will hurt you in the long run.

#include <stdio.h>

int main()
{
	float fa, ce;
	/* Declare your variables as floating variables.
	 * The reason being that you will want decimal answers.
	 */
	printf("***************************************************************\n");
	printf(" >> Celsius to Fahrenheit converter <<\n\n");
	/* Fahrenheit is spelled Fahrenheit, not Fahrenhiet. =)
	 */
	printf(" >> Enter temperature in Celsius = ");
	scanf("%f", &ce);
	/* "%f" has to be used instead of "%d".
	 * "%f" is for floating numbers, "%d" is for integers.
	 */
	fa=(ce*(float)9/5)+32;
	/* When you want a decimal answer to the division of two integers...
	 * Declare the first integer as floating!
	 */
	printf(" >> The temperature in Fahrenheit is %f\n", fa);
	/* Print the temperature (not tempurature - your typo) in Fahrenheit.
	 * You had it printing the original Celsius temperature.
	 */
	printf("***************************************************************\n");
	return 0;
	/* Use return 0
	 * Unless the code you gave was part of a loop or something...
	 */
}

-stettybet0

Edited by stettybet0, 30 April 2007 - 05:17 PM.

  • 0

#4
bluntknife

bluntknife

    Member

  • Topic Starter
  • Member
  • PipPip
  • 55 posts
ok thanks i have got in working now, there was a small typo in the code. instead of useing the correct fa i used ce.

many thanks

ps i have converted the whole program so that it use float bt i was wonding if you knew how limit the out come to teo decimal points?

Edited by bluntknife, 01 May 2007 - 10:17 AM.

  • 0

#5
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
If by "teo" you mean two, then:

printf(" >> The temperature in Fahrenheit is %0.2f\n", fa);

If by "teo" you mean ten, then:

printf(" >> The temperature in Fahrenheit is %0.10f\n", fa);

-stettybet0
  • 0

#6
bluntknife

bluntknife

    Member

  • Topic Starter
  • Member
  • PipPip
  • 55 posts
ok, sorry i did mean two but i was rushed when i write the reply.

thanks for your help stettybet0
  • 0

#7
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Heh, it's okay.

I'm glad I could help.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP