Thread: how to grab values from a line?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    how to grab values from a line?

    If I write 123456 or 1 2 3 4 5 6 or 1,2,3,4,5,6 and press enter. Or

    1 (I press enter)
    2 (enter)
    ....

    Is there a way I can catch every single digit and store them as an int? If so, what is that called? I dont need the code, I just want to know what its called so I can read about it, but example code will be appreciated

    For instance 123456 could be stored as int a[6] where a[0]=1 a[1]=2 and so on

    or possibly a=1 b=2 c=3 and so on.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Quote Originally Posted by mangekyou
    If I write 123456 or 1 2 3 4 5 6 or 1,2,3,4,5,6 and press enter.
    I would prefer different approaches for each. For 123456, I would read into a string and then convert each character in the string to its corresponding int value, say by subtracting the character '0'. For 1 2 3 4 5 6, it might be simpler to just call scanf in a loop, or to read as a string with fgets and then call sscanf in a loop. For 1,2,3,4,5,6, reading as a string then parsing with strtok might be good.

    Quote Originally Posted by mangekyou
    Or

    1 (I press enter)
    2 (enter)
    ....
    scanf in a loop sounds like it could work for this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    676
    Quote Originally Posted by mangekyou View Post
    If I write 123456 or 1 2 3 4 5 6 or 1,2,3,4,5,6 and press enter. Or

    1 (I press enter)
    2 (enter)
    ....

    Is there a way I can catch every single digit and store them as an int? If so, what is that called? I dont need the code, I just want to know what its called so I can read about it, but example code will be appreciated
    For "1 2 3 4 5 6" and "1,2,3,4,5,6" you can use a single scanf() call (scanf from scan function):

    Code:
    int a[6];
    
    scanf("%d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]); // if separated by spaces
    scanf("%d,%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]); // if separated by commas.
    In the first case, scanf() doesn't care if you type the values individually or not... In the second case, you must type the commas.

    If you type all values together, you must separate them by yourself (scanf() thinks 123456 is a single integer).

    Of course, if you have a variable quantities of numbers, using scanf() in a loop is a good idea if they are separated.
    Last edited by flp1969; 04-09-2019 at 06:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdin: Loop taking values - bug with new line character
    By deathmetal in forum C Programming
    Replies: 2
    Last Post: 08-13-2015, 11:22 AM
  2. How to treat values on a command line as seperate
    By metros in forum C Programming
    Replies: 7
    Last Post: 02-25-2010, 08:20 AM
  3. reading a string of hex values from command line
    By peeweearies in forum C Programming
    Replies: 7
    Last Post: 02-24-2009, 02:25 AM
  4. grab rgb
    By dP munky in forum Tech Board
    Replies: 4
    Last Post: 04-04-2003, 12:39 PM
  5. reading values from a line of text
    By Todd in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2002, 05:32 PM

Tags for this Thread