Problem reading input

This is a discussion on Problem reading input within the C Programming forums, part of the General Programming Boards category; Hey, I got a problem. I am to write a program that takes 4 different commands and performs different actions ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Problem reading input

    Hey, I got a problem.
    I am to write a program that takes 4 different commands and performs different actions for each individual command. The problem is that the commands are not of the same format.
    Here is what the commands take.

    command 1: char d, int, int.
    command 2: char a, int.
    command 3: char s.
    command 4: char e.

    Its all read in one line. I cant type 'd', hit enter and then prompt for 2 integers.
    What is a smart solution?
    I was thinking about reading it all in as a string, but then im not sure how to let the program know to look for an integer (integer could be 1 to 10000).

    Any thoughts would be appreciated

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, read it as a string. Get that much code written, and come back for more help.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    after you read it as a string, you can then tokenize it with strtok.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    // Get command from user (eg. fgets)
    // Don't forget to remove the trailing newline character
    Input = "command1 13 842\n"

    // Split command into words (eg. strtok)
    Word[0] = "command1"
    Word[1] = "13"
    Word[2] = "842"

    // See what command the user has entered (eg. if,else,then,case)
    // If the command has additional parameters, work with the tokenized strings (eg. atoi)
    Code:
    If Word[0] == "command1" Then
        Convert Word[1] to integer
        Convert Word[2] to integer
        DoStuff();
    Else If Word[0] == "command2" Then
        Convert Word[1] to integer
        DoStuff();
    Else If Word[0] == "command3" Then
        DoStuff();
    Else If Word[0] == "command4" Then
        DoStuff();
    Else
        UnknownCommand();
    End If
    Last edited by kpreston; 09-24-2008 at 06:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not reading file input :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 03:55 PM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 10:41 AM
  3. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  5. Problem with reading strings
    By goron350 in forum C++ Programming
    Replies: 8
    Last Post: 06-05-2005, 01:05 AM

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