What am I trying to establish here... jolly good question :eek:

Pretty simple piece of code really but can I get this wretched thing to do as its asked - can I as heck...

All it needs to do is display the type of clothing to be worn in certain weather. Anyway here goes..

char barometer;
 char rainfall;
 
 printf ( "Please enter barometer reading" );
 scanf ( "%s", &barometer );
 
 printf ( "Did it rain yesterday?" );
 scanf ( "%s", &rainfall );

 if ( barometer == storm )  
 {
  printf ( "he wears overcoat and hat\n" );
 }
 if ( barometer == rain )
 {
  printf ( "he wears raincoat and takes an umbrella\n" );
 }
 if ( barometer == fair )
 {
  printf ( "he wears light over-jacket and takes an umbrella\n" );
 }
 if ( barometer == very dry)
 {
  printf ( "he wears light over-jacket\n" );
 }
 if (( barometer == change ) && ( rainfall == yes )) 
 {
  printf ( "%s\n", fair );
 }
 if (( barometer == change ) && ( rainfall == no ))
 {
  printf ( "%s\n", rain );
 } 
 exit(0);
}

Thanks for looking... :cheesy:

Dani AI

Generated

Your snippet is tripping over three common mistakes: mismatched types for input, comparing to names that aren't defined as values, and printing identifiers as if they were strings. correctly flagged the input mismatch; is right that non-overlapping branches (else-if) avoid conflicting matches. Pick one clear representation (single-char flags, short strings, or a numeric pressure) and stick with safe input and comparisons.

If you only need a yes/no for rain, read a line and take the first character instead of trying to scan a string into a single char. This avoids leftover newlines and buffer overruns:

char line[32];
char rain = '\0';

fputs("Did it rain yesterday? ", stdout);
if (fgets(line, sizeof line, stdin))
    rain = line[0];

if (rain == 'y' || rain == 'Y')
    puts("Take an umbrella.");
else if (rain == 'n' || rain == 'N')
    puts("No umbrella needed.");

If barometer is a numeric reading, parse it and use threshold checks (else-if) so only one branch runs:

char line[64];
double hpa;

fputs("Barometer (hPa): ", stdout);
if (fgets(line, sizeof line, stdin) && sscanf(line, "%lf", &hpa) == 1) {
    if (hpa < 985.0) puts("Overcoat and hat");
    else if (hpa < 1003.0) puts("Raincoat + umbrella");
    else if (hpa < 1018.0) puts("Light jacket + maybe umbrella");
    else puts("Light jacket");
}

A few final tips: if you want named categories like "storm" or "rain" make them constants (enum or const strings) and compare accordingly (strcmp for strings). Never call printf with an identifier that isn't a char* (e.g., don't pass an enum or bare token to a %s). Compile with warnings enabled (gcc -Wall -Wextra) to catch type/format mismatches early.

Recommended Answers

All 2 Replies

>>scanf ( "%s", &rainfall );

%s wants a character array of at least 2 bytes. rainfall is not a character array, so the above will always fail to work correctly.

char rainfall[2]; // 1 byte for either Y or N and secnd byte for null terminator
scanf ( "%s", &rainfall );

There are probably other problems, but I stopped reading after that line.
[edit]barometer has the same problem. Where do you declare variables storm etc ? Post them too.[/edit]

Also, you probably want to compare barometer to a range, rather than a specific value. Perhaps something like:

// assuming storm is low, and better weather is higher
if(barometer <= storm)
// ...
else if(barometer <= rain)
// ...
else if(barometer <= fair)
/* etc... */

The ranges will be determined as the lowest category the barometer value fits into, and by using else if you won't have conflicts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.