Re: Can I detect "enter" key using scanf?

Discussion in 'C Programming' started by Dan Pop, Aug 6, 2003.

  1. Dan Pop

    Dan Pop Guest

    In <> (Abby) writes:

    >Currently I use scanf to get user's input.
    >
    > printf("Please enter your car's make: \n");
    > scanf("%s",&make);


    I bet &make is a mistake. If make is an array (presumably of char), it
    is wrong to use the & operator on it in most contexts. Read your book
    and/or the FAQ.

    >I would like to detect if user type nothing and just press enter, my
    >program will assign "toyota" as the default value. With above code,
    >when I press enter with NULL value, the program will go no where. It
    >still waiting for me to type something. How can I get through this?


    By reading the specification of scanf *carefully* in your C book.
    %s is seldom what you want, one of the reasons being already discovered
    by you: it doesn't allow the user to input an empty line.

    char make[20 + 1] = "";
    int rc;

    printf("Please enter your car's make: \n");
    rc = scanf("%20[^\n]%*[^\n]", make);
    if (rc >= 0) getchar();
    else /* the user pressed the EOF key */ ;

    >PS. Please forgive me if I post many questions lately. I'm having fun
    >learning C, but get stuck very often since I'm new.


    You're taking the wrong approach: limit your coding to the features you
    already have a firm grip upon, rather than playing with things you haven't
    already properly studied. Otherwise, you're simply wasting your time
    (and ours).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 6, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: Can I detect "enter" key using scanf?

    Ben Pfaff, Aug 5, 2003, in forum: C Programming
    Replies:
    0
    Views:
    689
    Ben Pfaff
    Aug 5, 2003
  2. bd
    Replies:
    0
    Views:
    1,649
  3. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    403
    Anders Matthiessen
    Jan 12, 2005
  4. M P
    Replies:
    1
    Views:
    483
  5. HopfZ
    Replies:
    0
    Views:
    576
    HopfZ
    Aug 28, 2006
Loading...

Share This Page