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 18-Apr-2006, 13:44
ajguerrero ajguerrero is offline
New Member
 
Join Date: Apr 2006
Posts: 3
ajguerrero is an unknown quantity at this point

Convert Fahrenheit to Celsius and Kelvin


my assignment is to write a program that asks for a temperature in fahrenheit, then spits out a value in celsius and kelvin. and ask again for a value in fahrenheit to convert. heres what ive got so far. by the way im using kernhigan and ritchie, with no experience in programming whatsoever. im finding that kernhigan and ritchie is not very good for me.

CPP / C++ / C Code:
#iclude < stdio.h >
#include <math.h >

main()
{
           float fahr, celsius, kelvin;
           int temp, cels, kelv

           temp = ' '
           printf("Please enter (in fahr) a temp\n(q to quit)\n");
           while (temp != 'q') {
                   temp = getc ( stdin );
                   putchar (temp);
                   cels = (temp-32.0) + (5.0/9.0);
                   kelv = 273.5 + ((temp - 32.0) + (5.0/9.0));
           printf("The temperature in Celsius is %6.1f\n" , cels);
           printf("The temperature in Kelvin is %6.1f\n" , kelv);
           }
           return = 0
}


also, my next assignment is to use the program above (when written correctly) and rewrite it so that it uses one function to conver from fahrenheit to celsius and another to convert from celsius to kelivin. any help would be appreceated
Last edited by LuciWiz : 24-Apr-2006 at 06:00. Reason: Please insert your C code between [c] & [/c] tags; Edited title (previous title was "newb question: probs with following program")
  #2  
Old 18-Apr-2006, 16:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: newb question: probs with following program


Quote:
Originally Posted by ajguerrero
my assignment is to write a program ...im using kernhigan and ritchie, with no experience in programming whatsoever
.......

I have heard a number of people say that, and I won't argue about what's best for you or anybody else. If you poke around on the web or at a bookstore you might find something more to your liking.
In the meanwhile...

You can't make up your own language: the "R" of K&R created the language, and you have to play by the rules. Look at examples of K&R and try to make yours look like the ones there. Type some of them into your computer and compile them. If something happens that you don't understand, then ask us about it.

Now for your program:
CPP / C++ / C Code:
#iclude < stdio.h >
#icude isn't part of the language, but #include is. Furthermore, the stuff between the angle brackets should be written exactly the way it was in K&R:

< stdio.h > might not work, but <stdio.h> surely will (assuming your compiler is properly installed on your system).

So the very first line has two errors. What did your compiler say when you gave your program to it?

When asking for help, here is the suggestion:

Show us (exactly) what messages the compiler gave you that you don't understand. Be precise: post the entire message into your post; don't paraphrase. Tell us what compiler you are using. Sometimes it makes a difference to people who are trying to help you.

Now, go back and clean up your program and try to get it to compile. If the compiler tells you something, read the message and see if it means something to you. Sometimes cleaning up the problem that caused the first message will make some of the others go away.

So, if your compiler says
Code:
Error E2048 y.c 1: Unknown preprocessor directive: 'iclude'
Then look at line 1: what is it complaining about?

Note that if you fix line 1, then some of the other messages will go away:

Code:
Error E2451 y.c 12: Undefined symbol 'stdin' in function main Warning W8065 y.c 12: Call to function 'getc' with no prototype in function main Warning W8065 y.c 13: Call to function 'putchar' with no prototype in function main Warning W8065 y.c 16: Call to function 'printf' with no prototype in function main

All of these are caused by the compiler not being able to get the information that line number 1 was supposed to convey. I know that this is all very obscure for people who have never tried such a thing before, but it's a learning process, right? (No one was born knowing this stuff, you know.)

My point is: try to understand. If you don't understand, then ask. There are a lot of very capable people here who are not only willing, but eager to help. Some of us even remember what it was like trying to get started!


So: the first step is to get the program to compile with no errors. (I strongly suggest that you eliminate warning messages as well as errors. At this point in the game, you don't know whether a warning is serious or not, so just get rid of 'em all, I say.)

Now, when the program compiles OK, the real fun begins: You should be able to see some output from the program. If it's not what you expect it to be, then you enter phase II: debugging.

Remember, you can't make up your own rules for usage any more than you can make up the rules for syntax. Look in K&R (or other resources) for examples of reading numerical values. Hint: getc() is not what most people would recommend at this point.

Regards,

Dave
  #3  
Old 19-Apr-2006, 06:58
davis
 
Posts: n/a

Re: newb question: probs with following program


Quote:
Originally Posted by ajguerrero
my assignment is to write a program that asks for a temperature in fahrenheit, then spits out a value in celsius and kelvin. and ask again for a value in fahrenheit to convert. heres what ive got so far. by the way im using kernhigan and ritchie, with no experience in programming whatsoever. im finding that kernhigan and ritchie is not very good for me.

CPP / C++ / C Code:
#iclude < stdio.h >
#include <math.h >

main()
{
           float fahr, celsius, kelvin;
           int temp, cels, kelv

           temp = ' '
           printf("Please enter (in fahr) a temp\n(q to quit)\n");
           while (temp != 'q') {
                   temp = getc ( stdin );
                   putchar (temp);
                   cels = (temp-32.0) + (5.0/9.0);
                   kelv = 273.5 + ((temp - 32.0) + (5.0/9.0));
           printf("The temperature in Celsius is %6.1f\n" , cels);
           printf("The temperature in Kelvin is %6.1f\n" , kelv);
           }
           return = 0
}


also, my next assignment is to use the program above (when written correctly) and rewrite it so that it uses one function to conver from fahrenheit to celsius and another to convert from celsius to kelivin. any help would be appreceated

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const static int FAHRENHEIT     = 0;
const static int CELCIUS        = 1;
const static int KELVIN	        = 2;
const static float FIVE_NINETHS = 5.0F/9.0F;
const static float NINE_FIFTHS  = 9.0F/5.0F;

float fahrenheit_to_celcius( const float fahrenheit )
{
    return (FIVE_NINETHS * (fahrenheit - 32.0F));
}

float fahrenheit_to_kelvin( const float fahrenheit )
{
    return (273.0F + fahrenheit_to_celcius( fahrenheit ));
}

float celcius_to_fahrenheit( const float celcius )
{
    return ( (NINE_FIFTHS * celcius) + 32.0F);
}

float celcius_to_kelvin( const float celcius )
{
    return (273.0F + celcius);
}

float kelvin_to_celcius( const float kelvin )
{
    return (kelvin - 273.0F);
}

float kelvin_to_fahrenheit( const float kelvin )
{
    return celcius_to_fahrenheit( kelvin_to_celcius( kelvin ) );
}

void print_temps( float temps[3] )
{
    printf( "%.3f degrees Fahrenheit\n%.3f degrees Celcius\n%.3f degrees Kelvin\n\n", temps[FAHRENHEIT], temps[CELCIUS], temps[KELVIN] );
}

int main()
{
    char value[32] = {0};
    float temps[3] = {0};

    char choice[2] = {0};

	printf( "Enter Temperature Value: " );
	fgets( value, 32, stdin );
	value[ -1 + strlen( value ) ] = 0;
	printf( "Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): " );
	fgets( choice, 2, stdin );
	switch( choice[0] )
	{
	    case 'f':
	    case 'F':
    		temps[FAHRENHEIT]   = atof( value );
    		temps[CELCIUS]	    = fahrenheit_to_celcius( temps[FAHRENHEIT] );
    		temps[KELVIN]	    = fahrenheit_to_kelvin( temps[FAHRENHEIT] );
    		break;
	    case 'c':
	    case 'C':
    		temps[CELCIUS]	    = atof( value );
    		temps[FAHRENHEIT]   = celcius_to_fahrenheit( temps[CELCIUS] );
    		temps[KELVIN]	    = celcius_to_kelvin( temps[CELCIUS] );
    		break;
	    case 'k':
	    case 'K':
    		temps[KELVIN]	    = atof( value );
    		temps[FAHRENHEIT]   = kelvin_to_fahrenheit( temps[KELVIN] );
    		temps[CELCIUS]	    = kelvin_to_celcius( temps[KELVIN] );
    		break;
	    default:
		printf( "...see ya!\n" );
		exit( 0 );
	}
	print_temps( temps );

    return 0;
}


Output:

Code:
Enter Temperature Value: 373 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): k 212.000 degrees Fahrenheit 100.000 degrees Celcius 373.000 degrees Kelvin Enter Temperature Value: 0 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): c 32.000 degrees Fahrenheit 0.000 degrees Celcius 273.000 degrees Kelvin Enter Temperature Value: 212 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): f 212.000 degrees Fahrenheit 100.000 degrees Celcius 373.000 degrees Kelvin Enter Temperature Value: 76 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): f 76.000 degrees Fahrenheit 24.444 degrees Celcius 297.444 degrees Kelvin Enter Temperature Value: 98.6 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): f 98.600 degrees Fahrenheit 37.000 degrees Celcius 310.000 degrees Kelvin Enter Temperature Value: 0 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): k -459.400 degrees Fahrenheit -273.000 degrees Celcius 0.000 degrees Kelvin Enter Temperature Value: 100 Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): q ...see ya!


Take a closer look at your conversion formulas...

CPP / C++ / C Code:
cels = (temp-32.0) + (5.0/9.0);
kelv = 273.5 + ((temp - 32.0) + (5.0/9.0));

...should be:

CPP / C++ / C Code:
cels = (temp-32.0) * (5.0/9.0); // convert Fahrenheit to Celcius
kelv = 273.5 + ((temp - 32.0) * (5.0/9.0)); // convert F to C to K

Also, cels and kelv should be floats, not ints, unless you want the value truncated!


:davis:
 
 

Recent GIDBlogMeeting the local Iraqis 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

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

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


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