Thread: Problem reading input

  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
    Chappell Hill, Texas
    Posts
    2,332
    Yes, read it as a string. Get that much code written, and come back for more help.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    42
    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 05: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, 02:55 PM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  3. Replies: 6
    Last Post: 04-28-2006, 12: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, 12:05 AM