Thread: Need help with reading numbers from the command line

  1. #1
    Registered User
    Join Date
    Apr 2006

    Need help with reading numbers from the command line

    Hi guys,

    I'm not sure how to word the question so I'll just show an example,

    main 25/3

    Now, here's what I want to do. I want to take the three numbers and use them in a switch statement. Here are some codes I've tried,

    sscanf(argv[1], "%d%d/%d", &i, &j, &k);
    This code didn't work correctly because when I tried to output the values, I got first number,25, and two garbage numbers.
    sscanf(argv[1], "%d/%d", &i, &j);
    This code works, but not in the way I want it to. It gives me 25 and 3.

    Is there a way to separate the 2 and 5 so I can use them individually?

    Any help is appreciated. Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    How is scanf supposed to know that you want 2 and 5 instead of 25? Unfortunately (really!), computers aren't that bright, so you have to work around things like this. For example, read the first number as a string:
    sscanf(argv[1], "%[^/]/%d", a, &j);
    But that introduces other problems, like how many digits are you expecting for the first number? Do you treat each digit as unique, or is some clairvoyance required? If it's the latter then you need to redesign.
    My best code is written with the delete key.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I have a suggestion! Make your parameters less complicated. sscanf() is not the problem. If you want three numbers to use, you can make main have three parameters:
    prog 2 5 3
    instead of some fraction you don't even use, which obscures how seemingly normal data is handled.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Loop through the argument looking at each character.
    If a digit, convert to a number and save.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Mar 2005
    I would go with Prelude's solution.

    Read what ever the input is as char array and then decode it to numbers and special char.

    the solution holds good if

    1. you are looking at only single digits.
    2. you know the size of the input.

    else go with the other option as space separated digits or numbers.

    if you could give the context in which you are using.. you might get better solution from the forum. :-)

    happy hacking..!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  2. reading numbers from input file
    By ziga in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2007, 06:23 AM
  3. Reading numbers from a text file
    By wolfindark in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2007, 01:57 PM
  4. character strings and reading in a file...
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 07-30-2002, 09:51 AM
  5. Prime Numbers
    By cmangel518 in forum C++ Programming
    Replies: 13
    Last Post: 04-30-2002, 11:51 PM