Regex or scanf()?

This is a discussion on Regex or scanf()? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm making a console-based connect four game as a beginner project, I want have the user input a command ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    5

    Regex or scanf()?

    Hi, I'm making a console-based connect four game as a beginner project, I want have the user input a command in this syntax, "drop <int column_number>" to drop a coin. The problem is how! I want to be able to seperate the number from the command a store it into a seperate variable - and if the syntax is not as expect, I do not want the program to crash, I want it to just ask again.

    I have tried something like below, which kind of works and then crashes straight after. My understanding of scanf() is limited because I do not have the MSDN library.
    PHP Code:
    int col;
    scanf("drop %d", &col);
    return 
    col-1
    My understanding is that where %d is, is the value that will be put into col - please correct me if I am wrong.
    Last edited by Avochelm; 05-08-2004 at 11:02 PM.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    what might be better is to use scanf("%s",buff); and read a string in. Then use sscanf to parse the string afterwards. sscanf is basically the same as scanf, except it does it with constant strings and doesn't get the data as input from the user.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    5
    If I do it like that, it essentially is still the same as getting the information from the user?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > My understanding of scanf() is limited because I do not have the MSDN library.
    Without a decent reference, any new function you come across will leave you guessing.
    Didn't something come with your compiler?

    As mentioned already (partly at least), use sscanf() to pull the input apart.
    Code:
    #include <stdio.h>
    int main ( ) {
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
            int drop;
            if ( sscanf( buff, "drop %d", &drop ) == 1 ) {
                printf( "You dropped item %d\n", drop );
            } else {
                printf( "I don't understand %s", buff );
            }
        }
        return 0;
    }
    Simply add more else if constructs to match all your other commands.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    My understanding of scanf() is limited because I do not have the MSDN library.
    MSDN

    EDIT: Of course, it's impossible to find anything using the search feature at MSDN. Go to google and do it like this:

    "site:msdn.microsoft.com scanf"

    That will find what you need.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    5
    Thanks, I'm messing with the code =D I don't understand some it, off course, but it works perfectly and I can always find out the details later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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