Thread: How do I delimit a single character with scanf?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    How do I delimit a single character with scanf?

    Code:
    #include <stdio.h>
    int main{
    char move;
    scanf ("%c",&move");
    switch (move)
    case w: {y-move +=1; movechar();break;}
    case a: {x-move -=1; movechar();break;}
    case s: {y-move -=1; movechar();break;}
    case d: {x-move +=1; movechar();break;}
    return 0;
    }
    Sorry, it's my first time here as a member but i've been using this site to selfteach C programming. Not sure if I posted the thread right but if I did make a mistake to the post please correct me.

    Im making a small game where it requires movement of a piece with (w,a,s,d) keys.
    I've read about delimiters in strings, where you can use it to stop scanning when a certain key is pressed.

    here's my question.
    how do I do that same operation with the line 4(scanf): if w,a,s, or d is pressed immediately enter the following key. (without physically pressing enter everytime. the key is immediately registered and moves on to the next line)
    Last edited by alex3064; 11-29-2012 at 09:47 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32

    Lightbulb

    Try using getchar() instead of scanf

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > (without physically pressing enter everytime. the key is immediately registered and moves on to the next line)
    Non-blocking input is very specific to your OS/Compiler.
    FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com
    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.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Does your compiler have the header conio.h?

    If so, use the getch function from it.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by Salem View Post
    > (without physically pressing enter everytime. the key is immediately registered and moves on to the next line)
    Non-blocking input is very specific to your OS/Compiler.
    FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com
    Ah thanks for the link. Did not find that faq i guess i was searching incorrectly.
    So with scanf, Its impossible. Ill try with getch

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I can see a few errors with the code you've posted.
    - Your definition for main lacks a parameter list.
    - You have a double quote after the '&move' expression.
    - Unless w, a, s, and d are #defined or members of an enum, you're not using a constant for each case statement.
    - You're using the '-' character for the identifiers x-move and y-move.

    Another thing you might look out for is declaring identifiers with file scope. In most cases it's better to design 'movechar' so that it uses a list of parameters instead of file-scoped variables.

    As for receiving unbuffered input from a terminal, you could try reading the documentation for your terminal. I suggest writing programs that work well regardless of what stdin device stdin is connected to. If you need to write interactive programs, you might consider using a library like SDL or *curses, although it's usually better to get a good grasp of standard C before learning to use such libraries.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    It might also be worth mentioning ncurses, you can read chapter 7 of the tutorial: Input functions

    So in your program, you could do something like:


    #include <ncurses.h>


    initscr();
    cbreak();


    ^to initialize, and once it's initialized, to read a character you can run:


    int ch; ch = getch();


    nCurses is one of my favorite libraries.
    Last edited by Andrew Thompson; 11-30-2012 at 02:07 AM. Reason: bad typos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to loop a single character?
    By spcx in forum C Programming
    Replies: 4
    Last Post: 08-23-2011, 10:54 PM
  2. Need help with Single Character
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 10-05-2010, 01:29 AM
  3. Single Character Input.
    By mintsmike in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2009, 08:16 AM
  4. get single character from string
    By deltaxfx in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 01:21 PM
  5. Delimit strings
    By petrainc in forum C Programming
    Replies: 2
    Last Post: 03-15-2005, 08:33 AM