Thread: BIG problem-O with a C program

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    BIG problem-O with a C program

    argh, no matter what i try i can't seem to get this code right... the output should look like this:

    % wizard

    Welcome to "Weather Wizard" Unit Convertor!
    Please enter values in 'ENGLISH' units:
    Enter Fahrenheit temperature: 86

    Enter pressure in inches: 29.53

    Please enter values in 'METRIC' units:
    Enter Celsius temperature: 20

    Enter pressure in millibars: 1000

    Wind Calculator
    Enter miles and feet traveled: 1 1000
    Enter number of minutes and seconds elapsed: 1 28

    Humidity Calculator
    Enter Fahrenheit air temperature and dew-point temp: 86 86

    **********

    English to metric conversion
    Temperature: 86 Fahrenheit 30 Celsius
    Pressure: 29.53 inches 1000 millibars

    Metric to English conversion
    Temperature: 20 Celsius 68 Fahrenheit
    Pressure: 1000 millibars 29.53 inches

    Wind Calculator results
    Wind: 71.36 feet/sec 49 mph 42 kt

    Humidity calculator results
    Temperature 86 degrees F dew point 68 degrees F Relative Humidity 55%

    ---------------MY CODE (so far)------------------------>

    /* Project 1 */

    #include <stdio.h>

    int main()
    {
    int Fahrenheit, Celsius, pressure, miles, feet, minutes, seconds, air, dew;
    float inches;

    printf("Welcome to \"Weather Wizard\" Unit Convertor!\n");

    printf("Please enter values in 'ENGLISH' units: \n");

    printf("Enter Fahrenheit temperature: ");
    scanf("%d", &Fahrenheit);

    printf("\n");

    printf("Enter pressure in inches: ");
    scanf("%.2f", &inches);

    printf("\n");

    printf("Please enter values in 'METRIC' units: \n");
    printf("Enter Celsius temperature: ");
    scanf("%d", &Celsius);

    printf("\n");

    printf("Enter pressure in millibars: \n");
    scanf("%d", &pressure);

    printf("\n");

    printf("Wind Calculator\n");
    printf("Enter miles and feet traveled: \n");
    scanf("%d %d", &miles, &feet);
    printf("Enter number of minutes and seconds elapsed: ");
    scanf("%d %d", &minutes, &seconds);

    printf("\n");

    printf("Humidity Calculator\n");
    printf("Enter Fahrenheit air temperatre and dew-point temperature: ");
    scanf("%d %d", &air, &dew);

    printf("\n");

    printf("**********\n");
    return 0;
    }

    ... i have all the temp conversions, etc. btw~

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try this:
    Code:
    #include <stdio.h> 
    
    int main(void) 
    { 
      int Fahrenheit, Celsius, pressure, miles, feet, minutes, seconds, air, dew; 
      float inches; 
      
      printf("Welcome to \"Weather Wizard\" Unit Convertor!\n"); 
      
      printf("Please enter values in 'ENGLISH' units: \n"); 
      
      printf("Enter Fahrenheit temperature: "); 
      scanf("%d", &Fahrenheit); 
      
      printf("\n"); 
      
      printf("Enter pressure in inches: "); 
      scanf("%.2f", &inches); 
      while ( getchar() != '\n' );
      
      printf("\n"); 
      
      printf("Please enter values in 'METRIC' units: \n"); 
      printf("Enter Celsius temperature: "); 
      scanf("%d", &Celsius); 
      
      printf("\n"); 
      
      printf("Enter pressure in millibars: "); 
      scanf("%d", &pressure); 
      
      printf("\n"); 
      
      printf("Wind Calculator\n"); 
      printf("Enter miles and feet traveled: "); 
      scanf("%d %d", &miles, &feet); 
      printf("Enter number of minutes and seconds elapsed: "); 
      scanf("%d %d", &minutes, &seconds); 
      
      printf("\n"); 
      
      printf("Humidity Calculator\n"); 
      printf("Enter Fahrenheit air temperatre and dew-point temperature: "); 
      scanf("%d %d", &air, &dew); 
      
      printf("\n"); 
      
      printf("**********\n"); 
      return 0; 
    }
    scanf was being touchy again, you need to consider what characters are remaining in the input stream before reading anything else or bad things tend to happen. Most of the time a floating newline will hurt interactive programs, so it's advisable to not use scanf at all.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    thanx Prelude~!!! That solved that problem... unfortunately, i'm having trouble with the conversion stuff... how would i insert all this in (and into the ints and floats):

    Temperature Conversion:
    TC = (TF - 32) * 5/9
    TF = 9/5 * TC + 32

    Air Conversion:
    1 inch of mercury = 33.86389 millibars
    1 millibar = 0.02953 inches of mercury

    Wind Conversion:
    1 mile = 5280 feet
    1 knot = 1.15 miles per hour

    Humidity Conversion:
    RH = 100 * ((112 - 0.1T + TD) / (112 + 0.9T))^8

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    here's my updated code (in the end it's a little loco...some stuff doesn't work

    #include <stdio.h>

    int main(void)
    {
    int Fahrenheit, Celsius, pressure, miles, feet, minutes, seconds, air, dew, TC, TF;
    float inches;

    printf("Welcome to \"Weather Wizard\" Unit Convertor!\n");

    printf("Please enter values in 'ENGLISH' units: \n");

    printf("Enter Fahrenheit temperature: ");
    scanf("%d", &Fahrenheit);

    printf("\n");

    printf("Enter pressure in inches: ");
    scanf("%.2f", &inches);
    while ( getchar() != '\n' );

    printf("\n");

    printf("Please enter values in 'METRIC' units: \n");
    printf("Enter Celsius temperature: ");
    scanf("%d", &Celsius);

    printf("\n");

    printf("Enter pressure in millibars: ");
    scanf("%d", &pressure);

    printf("\n");

    printf("Wind Calculator\n");
    printf("Enter miles and feet traveled: ");
    scanf("%d %d", &miles, &feet);
    printf("Enter number of minutes and seconds elapsed: ");
    scanf("%d %d", &minutes, &seconds);

    printf("\n");

    printf("Humidity Calculator\n");
    printf("Enter Fahrenheit air temperatre and dew-point temperature: ");
    scanf("%d %d", &air, &dew);

    printf("\n");

    printf("**********\n");

    TC = (Fahrenheit-32)*5/9;
    TF = 9/5*Celsius+32;

    printf("English to metric conversion: \n");
    printf("Temperature: %d Fahrenheit %d Celsius\n", Fahrenheit, TC);
    printf("Pressure: %.2f inches %d millibars\n", inches, pressure);

    printf("\n");

    printf("Metric to Englsih conversion: \n");
    printf("Temperature: %d Celsius %d Fahrenheit\n", Celsius, TF);
    printf("Pressure: %d millibars %f inches\n", pressure, inches);

    return 0;

    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >some stuff doesn't work
    Your call to scanf to read a value into inches is incorrect and causes problems. Change this

    scanf("%.2f", &inches);

    to this

    scanf("%f", &inches);

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    scanf was being touchy again, you need to consider what characters are remaining in the input stream before reading anything else or bad things tend to happen. Most of the time a floating newline will hurt interactive programs, so it's advisable to not use scanf at all.
    How else should we get the input then ?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered

    How else should we get the input then ?
    By reading it with fgets() into a char array, then validating it properly and converting it to a number if necessary. This is more long winded than using scanf(), but gives you greater control over the input and how errors are handled.

    There are plenty of examples of doing this on this forum, if you want to know more do a search for some.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    fgets reads input into a string from the standard input file descriptor (stdin) and sscanf reads the string and converts characters into the desired type of variable.

  9. #9
    Unregistered
    Guest

    awww

    aah... man... so I have to do all that to write a program that prints
    "Hello Unregistered.."

    i think i am gonna write a function myself and include it to some kind of a dot.h file to all my programs

    and i am going to call it

    neatget()

    :-)

    once I pass on the array, it will do the gets, fgets, sscanf and all the things that i have to do and return the string in the way i want it.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Than make sure you return a pointer to the string allocated on the free store rather than the function stack. The stack memory is released after the scope of the function terminates however memory on the free store has global scope, it does not terminate unless the programmer manually frees it (free(ptr)) or the program terminates.

    Use malloc

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so I have to do all that to write a program that prints
    You don't have to, just consider your input carefully. scanf was designed for formatted input and the user rarely formats it the way you want. So it's safer (if also longer) to foolproof your code. That's not to say that you should always use the method described above, that would be as silly as not using goto because everyone says it's bad. You then come back to considering the input, if you make it simple enough and make sure that everything is okay before using it then use whatever you feel most comfortable with.

    The reason that we recommend not using scanf is because everyone uses it and never checks that it succeeded or fails to handle erroneous input correctly. It's very easy to break scanf by not giving it what it was expecting, so this problem is encountered a great deal.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    hmm... i seem to have run-time error with this program.... can someone run it on their comp & see if it works for ya... thanx a bunch~!)

    #include <stdio.h>

    int main(void)
    {
    int Fahrenheit, Celsius, pressure, miles, feet, minutes, seconds, air, dew, TC, TF;
    float inches;

    printf("Welcome to \"Weather Wizard\" Unit Convertor!\n");

    printf("Please enter values in 'ENGLISH' units: \n");

    printf("Enter Fahrenheit temperature: ");
    scanf("%d", &Fahrenheit);

    printf("\n");

    printf("Enter pressure in inches: ");
    scanf("%f", &inches);
    while ( getchar() != '\n' );

    printf("\n");

    printf("Please enter values in 'METRIC' units: \n");
    printf("Enter Celsius temperature: ");
    scanf("%d", &Celsius);

    printf("\n");

    printf("Enter pressure in millibars: ");
    scanf("%d", &pressure);

    printf("\n");

    printf("Wind Calculator\n");
    printf("Enter miles and feet traveled: ");
    scanf("%d %d", &miles, &feet);
    printf("Enter number of minutes and seconds elapsed: ");
    scanf("%d %d", &minutes, &seconds);

    printf("\n");

    printf("Humidity Calculator\n");
    printf("Enter Fahrenheit air temperatre and dew-point temperature: ");
    scanf("%d %d", &air, &dew);

    printf("\n");

    printf("**********\n");

    printf("\n");

    TC = (Fahrenheit-32)*5/9;
    TF = 9/5*Celsius+32;

    printf("English to metric conversion: \n");
    printf("Temperature: %d Fahrenheit %d Celsius\n", Fahrenheit, TC);
    printf("Pressure: %.2f inches %d millibars\n", inches, pressure);

    printf("\n");

    printf("Metric to Englsih conversion: \n");
    printf("Temperature: %d Celsius %d Fahrenheit\n", Celsius, TF);
    printf("Pressure: %d millibars %.2f inches\n", pressure, inches);

    printf("\n");

    printf("Wind calculattor results\n");
    printf("Wind: %d feet/sec %d mph %d kt\n",feet/seconds, miles/(minutes/60), 1.15*miles);


    return 0;

    }

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    It seems to work for me.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >printf("Wind: %d feet/sec %d mph %d kt\n",feet/seconds, miles/(minutes/60), 1.15*miles);
    You are getting an error because you are dividing an integer by 0. Either change your calculation to protect against this or handle any special cases where minutes / 60 would become 0.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  3. Small program big problem :-(
    By Fibre Optic in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2005, 08:53 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM