Thread: newb question: probs with this program

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    newb question: probs with this program

    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.

    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

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    If that's your actual code you need to take your time and look through it.
    You have simple mistakes like misspelling include and forgetting semi colons.
    Other than that it's close, you may run into a problem trying to use temp as both your loop control and fahr input.

    a cleaned up version without the loop
    Code:
    #include <stdio.h>
    
    int main()
    {
        float fahr, celsius, kelvin;
    
        printf("Please enter (in fahr) a temp\n");
    
        scanf("%f", &fahr);
    
        celsius = (fahr-32.0) + (5.0/9.0);
        kelvin = 273.5 + ((fahr - 32.0) + (5.0/9.0));
    
        printf("The temperature in Celsius is %6.1f\n" , celsius);
        printf("The temperature in Kelvin is %6.1f\n" , kelvin);
    
        return 0;
    }

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    #iclude < stdio.h >     /*Include spelled wrong*/
    #include <math.h >
    
    main()     /*main returns an int  int main()*/ 
    {
    float fahr, celsius, kelvin;     /*These aren't used*/
    int temp, cels, kelv      /*Make these floats  Integers can't have decimals*/ /*Missing ;*/
    
    temp = ' '    /*This should be a number not a character*/ /*missing ;*/
    printf("Please enter (in fahr) a temp\n(q to quit)\n");
    while (temp != 'q') {    /*Temp is a float and will never equal q so just do while(1)*/
    temp = getc ( stdin );    /*You can use scanf(" %d",&temp);*/
    putchar (temp);     /*I'm assuming that this is for checking and can be removed*/
    cels = (temp-32.0) + (5.0/9.0);      /*You do not need the .0 at the end of all numbers*/
    kelv = 273.5 + ((temp - 32.0) + (5.0/9.0));     /*same thing as previous*/
    printf("The temperature in Celsius is %6.1f\n" , cels);
    printf("The temperature in Kelvin is %6.1f\n" , kelv);
    }
    return = 0  /*return 0;*/
    }
    Last edited by 00Sven; 04-18-2006 at 03:54 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Nice post 00Sven...

    The other thing I didn't understand in the original post is why the <math.h> file was needed. I didn't see any calculations or math functions that were out of the ordinary.

    And I took the use of decimals in the calculations as an attempt to cast the values (which were declared as 'int') to a decimal type?

    TB

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    [QUOTE]
    im finding that kernhigan and ritchie is not very good for me
    [\QUOTE]
    I must agree with you. Try to find some beginer book.

  6. #6
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I thought that that was what it was for too but that does not make a variable declared as an integer become a float. And if they are declared as floats then there is no need to have all of the ".0"s on the end of numbers.
    On math.h, I have seen a ton of beginners include this whenever doing any kind of mathematical calulation, which you don't.
    ~Sven
    Last edited by 00Sven; 04-19-2006 at 08:07 AM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Password Program Question
    By SirTalkAlot415 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:35 PM
  3. A question of the MIPS assembly program
    By ok_good in forum Tech Board
    Replies: 0
    Last Post: 05-03-2006, 10:13 PM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM