954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unable to see the wood for the trees...

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:

kissiwat
Newbie Poster
17 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

>>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]

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You