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:

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.