Thread: If statement wont check the second condition

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    If statement wont check the second condition

    I've tried retyping the code several times and didn't work for some reason the If wont accept both Q and q it just accepts Q.

    Code:
    #include <stdio.h>
    int main(void)
    {
    	printf("A menu will show up and you choose the number for the selection you want.\n\n");
    	system("pause");
    	//here should get the letter entered
    	printf("Press the menu key \"q\" or \"Q\" to open the menu.\n\n");
    	
    	//here is to check if the letter is Q or q
    	if (getchar() == 'Q' || getchar() == 'q')
    	{
    		printf("\t\tMod Menu");
    	}
    	else
    	{
    		printf("Didn't enter \"Q\" or \"q\".\n");
    	}
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you call getchar twice. Perhaps you should call it once, saving the result into a variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to save the return value of getchar in a variable and use that in your conditional.
    The way you have it now, you call getchar and if it doesn't return 'Q' then you call it again, where it probably gets the newline that the first getchar left in the stream.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    I did try saving it as a variable but I keep getting warning messages that says something like trying to match string with integer set by default?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What was the code that you tried, and what was the exact error message?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    The code was ALOT different than this, I saved it over with the new in the first post and I just wrote this to show you the message I was getting:

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	char menu[1];
    	
    	printf("A menu will show up and you choose the number for the selection you want.\n\n");
    	system("pause");
    	//here should get the letter entered
    	printf("Press the menu key \"q\" or \"Q\" to open the menu.\n\n");
    	scanf("%c", &menu);
    	//here is to check if the letter is Q or q
    	if (menu == 'Q' || menu == 'q')
    	{
    		printf("\t\tMod Menu");
    	}
    	else
    	{
    		printf("Didn't enter \"Q\" or \"q\".\n");
    	}
    	return 0;
    }
    13 11 C:\Dev-Cpp\My Codes\Simple mod menu.c [Warning] comparison between pointer and integer [enabled by default]
    13 26 C:\Dev-Cpp\My Codes\Simple mod menu.c [Warning] comparison between pointer and integer [enabled by default]

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Use a single char, not an array of char (not even a char array with only one element).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Salem View Post
    Use a single char, not an array of char (not even a char array with only one element).
    Well, you could use a char array with only one element but it'd be a bit silly.

  9. #9
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Took Salem advice and it works now.

  10. #10
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    by the way why u using system("pause"); ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using two GetAsyncKeyState a while statement with an or condition
    By Rob Fisher in forum Windows Programming
    Replies: 3
    Last Post: 01-11-2013, 04:59 AM
  2. IF Condition Check
    By nickman in forum C Programming
    Replies: 3
    Last Post: 09-06-2012, 07:03 AM
  3. if-else statement condition
    By YouMe in forum C Programming
    Replies: 16
    Last Post: 07-04-2012, 09:48 AM
  4. check for y condition
    By alperen1994 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 02:04 PM
  5. check every condition
    By alperen1994 in forum C Programming
    Replies: 9
    Last Post: 03-21-2009, 03:34 PM