Thread: help with if statement and ENTER

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    help with if statement and ENTER

    printf("enter choice: ");
    scanf("%d", &choice);

    if(choice==1)
    do this;
    if(choice==2)
    do that;
    if(choice==(Enter key))
    do something;

    How can i make so that when the user hits the enter key, it records that as a choice and does whatever is in that if statement, instead of just going to the next line and waiting for a proper input.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    When ENTER key is pressed, it returns a newline character ('\n').just check for the new line character and proceed.And make sure the variable yor collecting the input should be of type CHAR or UNSIGNED CHAR

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    you mean like this?

    char choice;

    printf("enter choice: ");
    scanf("%c", &choice);

    if(choice==1)
    do this;
    if(choice==2)
    do that;
    if(choice=='\n')
    do something;

    i tried that but it doesn't work. Or do you mean something else?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Hi xhoangx,

    i think the mistake u made is checking the values in if statement.if u want to compare some value that should be in single quotes(' ') or else it will take the equavalent character. This is working well.i'm using VC++ 6.0.

    Code:
    char choice;
    
    printf("enter choice: ");
    scanf("%c", &choice);
    
    if(choice=='1')
    do this;
    if(choice=='2')
    do that;
    if(choice=='\n')
    do something;
    Last edited by prasath; 12-05-2005 at 03:57 AM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by xhoangx
    printf("enter choice: ");
    scanf("%d", &choice);

    if(choice==1)
    do this;
    if(choice==2)
    do that;
    if(choice==(Enter key))
    do something;

    How can i make so that when the user hits the enter key, it records that as a choice and does whatever is in that if statement, instead of just going to the next line and waiting for a proper input.

    please use code tags guys. to post your code.

    yes you can do that. sample code
    Code:
    #include<stdio.h>
    
    int main()
    {
        int ch;
        
        scanf("%d",&ch);
        
        if(ch==1)
        printf(" Block 1 ");
        else if(ch==2)
        printf(" Block 2");
        else if(ch==3)
        printf("Block 3");
       
    }
    /* my ouput
    1
     Block 1
    */
    ssharish2005
    Last edited by ssharish2005; 12-05-2005 at 03:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf statement executed twice
    By ananddr in forum C Programming
    Replies: 4
    Last Post: 05-07-2009, 12:17 AM
  2. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  5. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM