Thread: Problem with input

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Problem with input

    Say if i had a user enter a character
    Code:
    printf("Enter operation code: ");
    scanf("%s", operCode);
    
    if(operCode == 's')
    {
    some code to run
    }
    
    if(operCode == 'i')
    {
    some code to run
    }
    
    if(operCode == 'u')
    {
    some code to run
    }
    
    if(operCode == 'p')
    {
    some code to run
    }
    
    if(operCode == 'q')
    {
    some code to run
    }
    now say none of those characters were entered, why does this always return true or have no effect reguardless if i entered a right caracter ???

    Code:
    if(operCode != 'i', 's', 'u', 'p', 'q')
    {
    some code to run
    }
    how would i tell it...if none of these letters weren't entered, then run the condition ?! i've tried using both ' ' and " "...i also tried having it run first and last but no luck

    thanks for the help

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    i'm not sure if I'm right, but could it be that you don't need ' ' in the if statement?

    and what do you mean by return true OR have no effect? if you entered the right char it will return true and if you don't enter the right char it will have no effect right...?

    i'm new with C so don't flame meh
    Last edited by vutek0328; 08-15-2006 at 10:43 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%s", operCode);
    Do you want a string or a character? Whether this is correct or not, and as a result, the rest of your code, depends on how operCode is defined. Most likely you want this:
    Code:
    char operCode;
    scanf ( "%c", &operCode );
    If it's a string, you want something like this:
    Code:
    char operCode[2];
    scanf ( "%1s", operCode );
    But then you'll have to use operCode[0] in your comparisons or compare with strings using strcmp.
    >if(operCode != 'i', 's', 'u', 'p', 'q')
    It doesn't work like that. You need to specify a comparison for each and every condition and link them with logical operators:
    Code:
    if ( operCode != 'i'
      && operCode != 's'
      && operCode != 'u'
      && operCode != 'p'
      && operCode != 'q' )
    {
      /* Some code to run */
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if(operCode != 'i', 's', 'u', 'p', 'q')
    this statement evaluates to 'q' and 'q' is alwais true.
    you want
    Code:
    if(operCode != 'i' && operCode != 's' && operCode !=  'u' && operCode !=  'p' && operCode !=  'q')
    Kurt

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    sweet that last one worked...thanks alot for the help people really appreciate it !

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Prelude
    >scanf("%s", operCode);
    It doesn't work like that. You need to specify a comparison for each and every condition and link them with logical operators:
    Code:
    if ( operCode != 'i'
      && operCode != 's'
      && operCode != 'u'
      && operCode != 'p'
      && operCode != 'q' )
    {
      /* Some code to run */
    }
    Or use a nice neat switch statement:
    Code:
    switch(operCode){
        case 's':
        case 'u':
        case 'p':
        case 'q':
            /* some code to run */
            break;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since we're at pointless variations, try this...
    Code:
    if( strchr( "siupq", operCode ) == NULL )
    {
        ...stuff...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 11:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM