Small q...

This is a discussion on Small q... within the C Programming forums, part of the General Programming Boards category; I have this code: Code: #include <stdio.h> main() { float cent, fahr; printf("Enter fehrenheit to be converted centigrade: "); scanf("%f", ...

  1. #1
    Unregistered
    Guest

    Small q...

    I have this code:

    Code:
    #include <stdio.h>
    main()
    {
    float cent, fahr;
    
    printf("Enter fehrenheit to be converted centigrade: ");
    scanf("%f", &fahr);
    cent = (5.0/9.0) * (fahr-32.0);
    printf("\n%3.0f fehrenheit = %6.1f centigrade\n", fahr, cent);
    if (cent > 38)
    {
            printf("Dangerously temperature level\n");
    }
    else if (cent < 35)
    {
            printf("Temperature is very low, Hypothermia alert!\n");
    }
    else
    {
            printf("Temperature level not dangerous, but monitor closly\n");
    
    }
    }
    the program works fine, but at ne of the prompts wen a user enters a letter, the program crashes, can sum1 help, i have tried while loops, but have not been successful

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    8
    i copied and pasted ur code. it couldnt change double values to float values. it ran find after that

    this was the warning after i compiled it:

    C:\Temp\Unregistered.c(8) : warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data

    the output wasnt even close to right:

    0 fehrenheit = -17.8 centigrade
    Temperature is very low, Hypothermia alert!
    Press any key to continue

    that was the programs original output, with the warning. no matter what farenheit temperature u put in, that was the answer.

    to fix your warning:

    double cent, fahr;



    hope i helped

  3. #3
    Unregistered
    Guest
    0 degrees fahrenheit = -18 degrees celsius is correct.
    You say it crashes when it expects a float and you enter a letter.
    You could enter a string and then do atof.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    8
    i dotn think u understood hehe, i could put 10000 F and it would still give me that output.

    and it didnt crash on mine either

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could use a combination of fgets() and sscanf().
    Code:
    #include <stdio.h>
    int main(void)
    {
    char s[20];
    int fields_scanned;
    float cent, fahr;
    
    do {
       printf("Enter fehrenheit to be converted centigrade: ");
       fgets(s,20,stdin);
       fields_scanned = sscanf(s,"%f", &fahr);
    } while (fields_scanned == 0);
    cent = (5.0/9.0) * (fahr-32.0);
    printf("\n%3.0f fehrenheit = %6.1f centigrade\n", fahr, cent);
    if (cent > 38)
    {
            printf("Dangerously high temperature level\n");
    }
    else if (cent < 35)
    {
            printf("Temperature is very low, Hypothermia alert!\n");
    }
    else
    {
            printf("Temperature level not dangerous, but monitor closely\n");
    
    }
    return 0;
    }

  6. #6
    Unregistered
    Guest

    Unhappy

    Sorry, guys the code I posted in my 1st post, works fine, I just tried it again.

    When I entered:

    1000 = Dangerously high temperature level
    100 = Temperature level not dangerous, but monitor closely
    -3 = Temperature is very low, Hypothermia alert!

    Think that is correct!!

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    no offense to anyone but can you please not speak in
    cl0se t0 l337?
    with the duz and ne s.
    I jost got the highest grade for an essay and grammar is like on my head and it is quite difficult to read since I stopped hanging out with people who speak in only l337
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 03:09 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21