Thread: How do I read individual numbers from an input??

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    How do I read individual numbers from an input??

    I have spent countless hours trying to figure this out and I am not getting any results!

    Say the program asks for an input:
    ie.)
    123456789

    How do I read every single number?
    ie.)
    First number: 1
    Second number: 2
    ...

    I've been using arrays, but the program stores the whole input as a whole and does not read them separately. Is it pointers? How do I do that with pointers? Please help. By getting this step down, I will be able to get a whole program down for sure.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you want to read character by character, then convert each character to the number value. Reading character by character can be accomplished by using getchar() in a loop and the conversion can be done by subtracting the character literal '0'.
    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
    Sep 2012
    Posts
    3
    Unfortunately, we are not allowed to use getchar().

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by shockem View Post
    Unfortunately, we are not allowed to use getchar().
    What are you allowed to use?

  5. #5
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    The input is put into an array of chars (Basically a String) am I right?

    Well then, couldn't you just do something like this?

    Code:
    for (ii = 0; ii < length; ii++)
    {
        number = string[ii];
        printf("%c", number);
    }
    Or you could even use a while loop. (while not the null character, '\0', which is at the end of every String.)


    I'm only a first year student, so I could very well be wrong, feel free to correct me if I'm wrong.
    Last edited by The Doctor; 09-03-2012 at 07:39 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Normal form is for (ii = 0; ii < length; ii++) ... not <=

  7. #7
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Oh yeah, I forgot. <= would put us past the length of the array. Edited my other post.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    That didn't work.
    This is so confusing. I didn't think it would be this tough. So, I am only allowed to use the <stdio.h> library and I can't use getchar or putchar. I know the input will be a string of characters. However, like laserlight mentioned, those characters need to be converted to integers.
    ie.) Input is 1293942
    Output:
    1
    2
    9
    3
    ...

    The reason why they can't be left as character strings is because I will need it to be integers so I can do some algorithms with them for the other part of my program. I just need a way to separate the individual numbers from the input.

    Here's where im stuck (SIZE is defined to be 5):
    Code:
    printf("Input: ");
    for (i=0; i<SIZE; i++)
    { 
    scanf("%d", &number[i]);
    }
    
    printf("Result: \n");
    for (i=0;i<SIZE; i++)
    {
    printf("%d", number[i]);
    printf("\n");
    }
    I understand the numbers being scanned are not characters. When I scan them using %s, I get a segmentation fault.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by shockem View Post
    When I scan them using %s, I get a segmentation fault.
    Do you use a char-array if you use "%s"?
    You want to read the whole sequence into the array (no for-loop needed, just one simple scanf()-call) and then just print each character on its own line.

    Bye, Andreas

  10. #10
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    Post

    Try creating an int array You would convert each char from the char array into an int (I think you could use the atoi() function ), and place it in the appropriate int array index. I would think that would work, using something similar to what I wrote earlier.

    Again, I may be wrong, but it makes sense to me right now.

    EDIT: Something like this?

    Code:
    #include <string.h>
    
    /*DEFINE EVERYTHING HERE */
    
    for (ii = 0; ii < length; ii++)
    
    {
    
        charNum = input[ii];
         /* input would be an array of chars here. */
    
        tempInt = atoi( charNum); /*Converts to an int; I'm pretty sure this is in String.h*/
        
        intArray[ii] = tempInt;
         /* We now have an array of the integer values each of the chars represent */
    }
    
    /*DO WHATEVER*/
    Last edited by The Doctor; 09-04-2012 at 12:28 AM.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    atoi() is in stdlib.h and expects a pointer to char as argument. In your example "charNum" is a char and thus will crash atoi().

    Bye, Andreas

  12. #12
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Ahh, okay. Yeah, listen to him. As I said, I'm still learning myself.
    Last edited by The Doctor; 09-04-2012 at 02:24 AM.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since you're allowed to use scanf() there is a simple answer. You need a format string like this:

    % - sign that a conversion is coming
    1 - field size of input
    d - sign that the input should be a decimal integer

    Then use scanf to put a digit into every array cell. I don't think there is much else that needs to be done, you just needed to find the correct format string. To study format strings, I recommend using the web. Here :
    Last edited by whiteflags; 09-04-2012 at 02:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 02:29 PM
  2. Read in numbers from file
    By ElNino in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 01:47 PM
  3. how to read numbers in a better way?
    By kcfung in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2006, 03:57 PM
  4. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 06:32 AM
  5. check individual characters on input
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 09:56 PM