# BIG problem-O with a C program

• 07-17-2002
JohnMayer
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~
• 07-17-2002
Prelude
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
• 07-17-2002
JohnMayer
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
• 07-17-2002
JohnMayer
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;

}
• 07-17-2002
Prelude
>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
• 07-17-2002
Unregistered
Quote:

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 ?
• 07-18-2002
Hammer
Quote:

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.
:)
• 07-18-2002
Troll_King
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.
• 07-18-2002
Unregistered
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.
• 07-18-2002
Troll_King
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
• 07-18-2002
Prelude
>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
• 07-19-2002
JohnMayer
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~!:o)

#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;

}
• 07-19-2002
The Dog
It seems to work for me.
• 07-20-2002
Prelude
>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