1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    Reading and analyzing variable length user input

    Hello all,

    I'm a programming newb. I need to write a program in C that performs actions on a set of 6 matrices(4x4).

    The main issue is not the actions themselves(addition, subtraction, etc.) but reading input from the user and acting accordingly.

    For example : read_mat A 5,6,2,
    means input the ints into matrix A

    mul_mat A,B - multiply matrices A,B

    I'm instructed to look out for input mistakes like read_mat Z(Z doesn't exist)
    or read_mat A,B,5,6,7("Wrong parameters, second parameter should be a real number")
    or mul_mat A,5("5 isn't a matrix name")
    or kkkk A,B("kkkk isn't a command")

    How do i go about doing this?
    Is there any standardized way of analyzing what the user is typing in?
    I tried tinkering with scanf but couldn't figure out how to account for the multitude of possibilities in what the user might type.

    Any help will be much appreciated.

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Rep Power
    A good first step would be to write a program that asks for input and then just display that. Once you get to that level, you take a step backwards and just write down a set of instructions in your native language and consider how to bend your program to fit.


    1. First instruct the user on std output to enter a random string of characters followed by a carriage return.
    2. Read a line from std input.
    3. Write that line to std output.

    If you first get yourself organized and demonstrate some willingness to work the problem, you'll get more and better assistance than if you simply post your homework assignment. If there was simple "standard way" to do it, the world wouldn't need any programmers.
    I no longer wish to be associated with this site.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    You must MUST define the input. What's legal, what's not. It looks like it is sufficiently simple. Describe it in BNF.

    Example at this link

    You'll need a lexical analyzer to break the input into tokens. Then you'll need a finite state machine implementing the grammar. I'd use flex and bison. These write c code that will nicely integrate with the rest of your system.

    The rest, you've claimed and I believe you, the linear algebra is easy.

    I believe the examples in the bison manual are sufficient. You aren't evaluating code conditionally, so you don't need to retain the parse tree. You will need symbol tables.

    I hope the project isn't due tomorrow. Oh, programming is new to you. Yeah, good luck.

    Comments on this post

    • jwdonahue disagrees : Not helpful.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo