Thread: Getting value from user

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Getting value from user

    Hi,
    I am new in the forum and i have a little question for you,
    in my program i have to use a "value" that the user must be entered as

    ./"name of executable" -n "value"

    How can i store and use that "value" value

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 2) > Accessing command line parameters/arguments
    FAQ > Prelude's Corner > Command line input
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You need to parse the stuff passed to main():
    Code:
    int main(int argc, char *argv[])
    argc is the number of elements in the argv array. Each element of argv is one command line token as the shell understands it: for POSIX shells, the first entry (argv[0]) is the part of the command line that called your program ("./executable"), each subsequent entry is one command line argument, separated by space, unless the space is escaped or within quotes. In your example call, argv[1] would be "-n" and argv[2] "value".
    If the value is supposed to be a number, then you must parse its string representation, usually with strtol or atoi.

    In addition, POSIX systems offer the program options functions (forgot the exact name), which parses standard UNIX command line options (e.g. sets flags with the - and -- stuff).
    Along the same lines, Boost has the Program Options library, which does the same, but in a C++ way instead of C.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    argc is the number of elements in the argv array. Each element of argv is one command line token as the shell understands it: for POSIX shells, the first entry (argv[0]) is the part of the command line that called your program ("./executable"), each subsequent entry is one command line argument, separated by space, unless the space is escaped or within quotes. In your example call, argv[1] would be "-n" and argv[2] "value".
    And argv[3] would be NULL.

    Code:
    int main(int argc, char *argv[])
    That's usually how it's written, but you might see it like this:
    Code:
    int main(int argc, char **argv)
    If the value is supposed to be a number, then you must parse its string representation, usually with strtol or atoi.
    That's in C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Indeed. Though I tend to parse numbers in C++ with atoi too, unless I'm allowed to use Boost and can use lexical_cast. Setting up a local stringstream is just too much hassle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:15 AM
  2. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  3. Add/Delete Remotely a user
    By Scarvenger in forum Windows Programming
    Replies: 5
    Last Post: 03-24-2008, 08:36 AM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM