Characters in If Statements

This is a discussion on Characters in If Statements within the C Programming forums, part of the General Programming Boards category; I was just writing some more for my program and I tried to to this: Code: printf("Are you sure you ...

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Characters in If Statements

    I was just writing some more for my program and I tried to to this:

    Code:
    	printf("Are you sure you want to clear the database? (Y or N) ");
    	fgets(buff,sizeof(buff),stdin);
    	sscanf(buff,"%c", &ques);
    	if((ques !='y)' || (ques !='Y'))
    But even if I type Y , it does what is in the if statement's brackets. So how do you compare characters correctly?
    Last edited by Padawan; 04-03-2004 at 04:39 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Quote Originally Posted by Padawan
    I was just writing some more for my program and I tried to to this:
    Code:
    	if((ques !='y)' || (ques !='Y'))
    i dont know if you just mistyped it in here, but you have a little typo right there it should be if ques != 'y' not 'y)' you included the bracket within the single quotes, y) is not a recognized character in C.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    assuming you are directly cut and paste, here's an error
    if((ques !='y)' || (ques !='Y'))
    also, ques != 'y' || ques != 'Y' will give you true no matter what you enter...
    if ques == 'y', then ques != 'Y' because 'y' != 'Y'...
    remember, || only gives you false if both sides are false...
    use &&

  4. #4
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    I didn't cut and paste and my code doesn't have that little typo. Sorry. But tzu, that solved my problem. Thanks. I think my brain needs a rest LOL. But I'm just soooo determined to get good at C.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Using sscanf for this is a bit of an overkill. Try something like this
    Code:
     #include <stdio.h>
     #include <ctype.h>
     
     int main(void)
     {
       char buff[BUFSIZ];
       
       printf("Are you sure you want to clear the database? (Y or N) ");
     
       if (fgets(buff, sizeof(buff), stdin) == NULL)
       {
         /* Handle input error */
       }
     
       if (toupper(buff[0]) == 'Y')
       {
         /* User entered y or Y */
         puts ("thanks!");
       }
       
       return 0;
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ohhh. Never thought of that. Thanks for the suggestion Hammer.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    how about ditching fgets altogether and just use:
    Code:
    int c;
    
    if ( ( c=toupper( getchar() ) ) != 'Y')

  8. #8
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Padawan
    I was just writing some more for my program and I tried to to this:

    Code:
    	printf("Are you sure you want to clear the database? (Y or N) ");
    	fgets(buff,sizeof(buff),stdin);
    	sscanf(buff,"%c", &ques);
    	if((ques !='y)' || (ques !='Y'))
    But even if I type Y , it does what is in the if statement's brackets. So how do you compare characters correctly?


    The below code should work...

    Code:
    	printf("Are you sure you want to clear the database? (Y or N) ");
    	fgets(buff,sizeof(buff),stdin);
    	sscanf(buff,"%c", &ques);
    	if((ques !='y)' && (ques !='Y'))

    I too used to get confused with this logic before.. the reason is we think in English...(some ppl might say we have to)... think about the logic...


    if((ques !='y)' || (ques !='Y'))


    if you enter y the first part of the sattement is false and since its and the second part of the statement is true and the condition is executed.......

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by vasanth
    The below code should work...

    Code:
    	printf("Are you sure you want to clear the database? (Y or N) ");
    	fgets(buff,sizeof(buff),stdin);
    	sscanf(buff,"%c", &ques);
    	if((ques !='y)' && (ques !='Y'))
    That darn cut and paste!

  10. #10
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    ahhh dint notice that error in his code.. cust and paste is evil

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by nonpuz
    how about ditching fgets altogether and just use:
    Code:
    int c;
    
    if ( ( c=toupper( getchar() ) ) != 'Y')
    Because if you don't handle the newline that getchar kindly leaves behind then any further input will probably have issues. Of course, you could use a common hack to remove extraneous characters, but fgets is a little more elegant.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21