Thread: Help with if statements

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Help with if statements

    Hi,
    I've written a program where everything works fine except one thing. The program is a series of questions and 'if statements' based on the answers.

    The only thing not working is snow = getchar(), water = getchar(), and muchsnow = getchar(), never get executed. The questions above them get printed, followed by the next question on the list which meets the requirements for the if statements. For the life of me I cannot figure out why this is happening. Any help would be appreciated.

    Code:
    include "main.h"
    
    int main()
    {
    	int season, sky;
    	char water = 'b', snow = 'y', muchsnow = 'b', temp = 'b';
    	printf("What season is it?: \n1. Winter\n2. Spring\n3. Summer\n4. Autumn\nPlease Enter the number corresponding with your selection.");
    	scanf("%i", &season);
    	printf("Is it sunny or cloudy?: \n1. Sunny\n2.Cloudy\nPlease enter your selection: ");
    	scanf("%i", &sky);
    	if(sky == 2)
    	{
    		if(season == 1)
    		{
    			printf("Is it snowing?: y/n ");
    			snow = getchar();
    		}
    		if(season != 1)
    		{
    			printf("Is it raining?: y/n ");
    			water = getchar();
    		}
    	}
    	if(season == 1)
    	{
    		printf("Is there alot of snow on the ground?: y/n ");
    		muchsnow = getchar();
    	}
    	printf("Is the tempreture normal for the season: y/n");
    	temp = getchar();
    	
    	if(((season == 1)&&(sky==1)&&(muchsnow = 'y'))||((season == 3)&&(sky ==1)))
    	{
    		printf("You should wear sunglasses");
    	}
    	if(((season == 2)||(season == 4))&&(sky == 2))
    	{
    		printf("You might want to bring a jacket");
    	}
    	if(water=='y')
    	{
    		printf("You should bring an umbrella");
    	}
    	if(((season == 2)||(season == 4))&&(sky ==1))
    	{
    		printf("You should bring a hat");
    	}
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course they get executed. Input to a console program (as opposed to a whiz-bang GUI program) is always buffered (i.e. you can type, backspace, edit, and whatever; the input isn't sent to your program until you press enter). For that reason, your program will only wait for input if there isn't already input in the buffer waiting for it. Since in this case it is impossible to reach those getchar() statements without leaving input in the buffer (for scanf("%i") must always leave input in the buffer), the program doesn't wait for new input but uses the input that it is waiting for it (in this case, enter-key).

    [EDIT for the pedantic: scanf("%i") always leaves input in the buffer in interactive mode; if you've redirected your standard input, then you may have EOF instead of newline or space or whatever at the end. If you don't know what "redirecting your standard input" even is, then that means you're not pedantic.]

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What tabstop is *trying* to say is that when you press 1 or 2 for sky... you hit Enter to move on.... notice that's 2 keystrokes.

    Scanf gets the number out of the buffer for you ...but... it leaves the Enter key in there.

    So your code proceeds to either snow or water... getchar() says "oh look there's something here" and promptly fetches the Enter key (\n) and moves on.

    It is executing the statement but it's not doing what you expected.

    Here's a little trick for you... replace all your getchar() and scanf() calls with this...
    Code:
    scanf(" %c",&variable);
    Note the space between the " and the % sign... that will tell scanf() to ignore whitespace (including \n) before waiting for you to type in your char and hit enter.
    Last edited by CommonTater; 08-17-2011 at 08:38 PM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Thanks so much. I never would have figured it out and I didn't even know what to google. It works great now.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why do you have a header file named main?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mjskolly View Post
    Thanks so much. I never would have figured it out and I didn't even know what to google. It works great now.
    It's one of those semi-documented things... But don't thank me... Thank the gang here, they're the ones who showed to me....

    Generally you can just Google the function ... scanf() in this case... you'll get tons of hits.

    scanf - Wikipedia, the free encyclopedia

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Why do you have a header file named main?
    Awww shucks Andrew... why not?

    I do that sometimes with my includes and defines in it...

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Awww shucks Andrew... why not?
    I think the operative question here is why?

    Quote Originally Posted by CommonTater View Post
    I do that sometimes with my includes and defines in it...
    Well, you're Canadian; 'nuff said.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The answer to why is most likely "I made a file called main.c and so my IDE put `#include "main.h"' at the top of the file for me".

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No kidding? Well, you learn something new everyday.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    why I have main.h

    2 reasons,
    The first being the teacher requires that we have a header file and the one in the first sample he gave us was main.h so I've been using it ever since. Its easy to remember.

    The other reason might be because I'm Canadian.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by mjskolly View Post
    2 reasons,
    The first being the teacher requires that we have a header file and the one in the first sample he gave us was main.h so I've been using it ever since. Its easy to remember.
    Hmm....ok. Fair enough, I guess.

    Quote Originally Posted by mjskolly View Post
    The other reason might be because I'm Canadian.
    Lol....I was right!

    See Tater, I told you!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statements :P
    By Pyroteh in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2005, 01:37 PM
  2. how to 'and' statements
    By chris285 in forum C++ Programming
    Replies: 11
    Last Post: 04-29-2003, 04:18 PM
  3. IF Statements ?
    By survivor in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 11:32 AM
  4. for statements
    By Lyanette in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2003, 07:15 PM
  5. Help with IF statements
    By boontune in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2003, 08:26 AM

Tags for this Thread