C - Filling array from specific single line input












0















I am a novice and for my school programming exercise I need to make integer array from specified input. Input look like this:



1000: { 250, 500, 750 } (enter)



My very basic code is capable to scan only numbers separated with whitespace.



#include <stdio.h>
#include <stdlib.h>
#define LEN 10
long int array[LEN];

int main()
{
long int i;

for (i =0; i < LEN; i++)
{
scanf("%li", &array[i]);
}

return 0;
}


I have a static array and I need to fill it with numbers in {} brackets. The number before ":" symbol (1000 in this case) I could scan as a single variable or as a 0th element of array. Sould I use some modified scanf? But I think the way here is some while cycle with scanf. Sometimes is array bigger than amout of given numbers so I need to end cycle with "}" symbol. Thanks for ideas.










share|improve this question

























  • scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

    – xing
    Nov 24 '18 at 15:43











  • Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

    – xing
    Nov 24 '18 at 15:46











  • Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

    – tvrzna
    Nov 25 '18 at 12:00











  • After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

    – xing
    Nov 25 '18 at 12:47











  • Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

    – xing
    Nov 25 '18 at 12:50
















0















I am a novice and for my school programming exercise I need to make integer array from specified input. Input look like this:



1000: { 250, 500, 750 } (enter)



My very basic code is capable to scan only numbers separated with whitespace.



#include <stdio.h>
#include <stdlib.h>
#define LEN 10
long int array[LEN];

int main()
{
long int i;

for (i =0; i < LEN; i++)
{
scanf("%li", &array[i]);
}

return 0;
}


I have a static array and I need to fill it with numbers in {} brackets. The number before ":" symbol (1000 in this case) I could scan as a single variable or as a 0th element of array. Sould I use some modified scanf? But I think the way here is some while cycle with scanf. Sometimes is array bigger than amout of given numbers so I need to end cycle with "}" symbol. Thanks for ideas.










share|improve this question

























  • scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

    – xing
    Nov 24 '18 at 15:43











  • Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

    – xing
    Nov 24 '18 at 15:46











  • Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

    – tvrzna
    Nov 25 '18 at 12:00











  • After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

    – xing
    Nov 25 '18 at 12:47











  • Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

    – xing
    Nov 25 '18 at 12:50














0












0








0








I am a novice and for my school programming exercise I need to make integer array from specified input. Input look like this:



1000: { 250, 500, 750 } (enter)



My very basic code is capable to scan only numbers separated with whitespace.



#include <stdio.h>
#include <stdlib.h>
#define LEN 10
long int array[LEN];

int main()
{
long int i;

for (i =0; i < LEN; i++)
{
scanf("%li", &array[i]);
}

return 0;
}


I have a static array and I need to fill it with numbers in {} brackets. The number before ":" symbol (1000 in this case) I could scan as a single variable or as a 0th element of array. Sould I use some modified scanf? But I think the way here is some while cycle with scanf. Sometimes is array bigger than amout of given numbers so I need to end cycle with "}" symbol. Thanks for ideas.










share|improve this question
















I am a novice and for my school programming exercise I need to make integer array from specified input. Input look like this:



1000: { 250, 500, 750 } (enter)



My very basic code is capable to scan only numbers separated with whitespace.



#include <stdio.h>
#include <stdlib.h>
#define LEN 10
long int array[LEN];

int main()
{
long int i;

for (i =0; i < LEN; i++)
{
scanf("%li", &array[i]);
}

return 0;
}


I have a static array and I need to fill it with numbers in {} brackets. The number before ":" symbol (1000 in this case) I could scan as a single variable or as a 0th element of array. Sould I use some modified scanf? But I think the way here is some while cycle with scanf. Sometimes is array bigger than amout of given numbers so I need to end cycle with "}" symbol. Thanks for ideas.







c arrays input scanf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:50







tvrzna

















asked Nov 24 '18 at 14:26









tvrznatvrzna

11




11













  • scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

    – xing
    Nov 24 '18 at 15:43











  • Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

    – xing
    Nov 24 '18 at 15:46











  • Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

    – tvrzna
    Nov 25 '18 at 12:00











  • After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

    – xing
    Nov 25 '18 at 12:47











  • Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

    – xing
    Nov 25 '18 at 12:50



















  • scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

    – xing
    Nov 24 '18 at 15:43











  • Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

    – xing
    Nov 24 '18 at 15:46











  • Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

    – tvrzna
    Nov 25 '18 at 12:00











  • After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

    – xing
    Nov 25 '18 at 12:47











  • Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

    – xing
    Nov 25 '18 at 12:50

















scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

– xing
Nov 24 '18 at 15:43





scanf can scan text. A format string of "%ld : {%ld" would scan a long integer, optional whitespace, a colon, optional whitespace, a left brace and another long integer.

– xing
Nov 24 '18 at 15:43













Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

– xing
Nov 24 '18 at 15:46





Check the return of scanf. The above example will return 2 if two long integers are scanned. If the return is 1, 0 or EOF something went wrong.

– xing
Nov 24 '18 at 15:46













Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

– tvrzna
Nov 25 '18 at 12:00





Thanks for reply, but I don't understand, how to fill the array with this scanf. I know, how to scan specific number of integers in brackets into variables. But I need to make array from the numbers in brackets. And I don't know the amout of numbers in brackets. They allways give me a random amout of integers in brackets.

– tvrzna
Nov 25 '18 at 12:00













After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

– xing
Nov 25 '18 at 12:47





After the first scanf in my comment, the remaining values are in a pattern that can be scanned in a loop. A comma and a long int...

– xing
Nov 25 '18 at 12:47













Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

– xing
Nov 25 '18 at 12:50





Again check the return from scanf. When scanning for a comma and a long int, the right brace } will cause the scan to fail and return zero.

– xing
Nov 25 '18 at 12:50












1 Answer
1






active

oldest

votes


















0














Consider using fgets to read a line. Parse the line with sscanf. The %n specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.



#include <stdio.h>
#include <stdlib.h>

#define SIZE 4000
#define LIMIT 500

int main( void) {
char line[SIZE] = "";
char bracket = 0;
long int value[LIMIT] = { 0};
int result = 0;
int input = 0;
int offset = 0;
int span = 0;

printf ( "enter values x:{w,y,...,z}n");
if ( fgets ( line, sizeof line, stdin)) {
//scan for long int, optional whitespace, a colon,
//optional whitespace, a left brace and a long int
if ( 2 == ( result = sscanf ( line, "%ld : {%ld%n", &value[0], &value[1], &offset))) {
input = 1;
do {
input++;
if ( LIMIT <= input) {
break;
}
//scan for optional whitespace, a comma and a long int
//the scan will fail when it gets to the right brace }
result = sscanf ( line + offset, " ,%ld%n", &value[input], &span);
offset += span;//accumulate processed characters
} while ( 1 == result);
//scan for optional space and character ie closing }
sscanf ( line + offset, " %c", &bracket);
if ( '}' != bracket) {
input = 0;
printf ( "line was not terminated with }n");
}
}
}
else {
fprintf ( stderr, "fgets EOFn");
return 0;
}

for ( int each = 0; each < input; ++each) {
printf ( "value[%d] %ldn", each, value[each]);
}

return 0;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459142%2fc-filling-array-from-specific-single-line-input%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Consider using fgets to read a line. Parse the line with sscanf. The %n specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.



    #include <stdio.h>
    #include <stdlib.h>

    #define SIZE 4000
    #define LIMIT 500

    int main( void) {
    char line[SIZE] = "";
    char bracket = 0;
    long int value[LIMIT] = { 0};
    int result = 0;
    int input = 0;
    int offset = 0;
    int span = 0;

    printf ( "enter values x:{w,y,...,z}n");
    if ( fgets ( line, sizeof line, stdin)) {
    //scan for long int, optional whitespace, a colon,
    //optional whitespace, a left brace and a long int
    if ( 2 == ( result = sscanf ( line, "%ld : {%ld%n", &value[0], &value[1], &offset))) {
    input = 1;
    do {
    input++;
    if ( LIMIT <= input) {
    break;
    }
    //scan for optional whitespace, a comma and a long int
    //the scan will fail when it gets to the right brace }
    result = sscanf ( line + offset, " ,%ld%n", &value[input], &span);
    offset += span;//accumulate processed characters
    } while ( 1 == result);
    //scan for optional space and character ie closing }
    sscanf ( line + offset, " %c", &bracket);
    if ( '}' != bracket) {
    input = 0;
    printf ( "line was not terminated with }n");
    }
    }
    }
    else {
    fprintf ( stderr, "fgets EOFn");
    return 0;
    }

    for ( int each = 0; each < input; ++each) {
    printf ( "value[%d] %ldn", each, value[each]);
    }

    return 0;
    }





    share|improve this answer




























      0














      Consider using fgets to read a line. Parse the line with sscanf. The %n specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.



      #include <stdio.h>
      #include <stdlib.h>

      #define SIZE 4000
      #define LIMIT 500

      int main( void) {
      char line[SIZE] = "";
      char bracket = 0;
      long int value[LIMIT] = { 0};
      int result = 0;
      int input = 0;
      int offset = 0;
      int span = 0;

      printf ( "enter values x:{w,y,...,z}n");
      if ( fgets ( line, sizeof line, stdin)) {
      //scan for long int, optional whitespace, a colon,
      //optional whitespace, a left brace and a long int
      if ( 2 == ( result = sscanf ( line, "%ld : {%ld%n", &value[0], &value[1], &offset))) {
      input = 1;
      do {
      input++;
      if ( LIMIT <= input) {
      break;
      }
      //scan for optional whitespace, a comma and a long int
      //the scan will fail when it gets to the right brace }
      result = sscanf ( line + offset, " ,%ld%n", &value[input], &span);
      offset += span;//accumulate processed characters
      } while ( 1 == result);
      //scan for optional space and character ie closing }
      sscanf ( line + offset, " %c", &bracket);
      if ( '}' != bracket) {
      input = 0;
      printf ( "line was not terminated with }n");
      }
      }
      }
      else {
      fprintf ( stderr, "fgets EOFn");
      return 0;
      }

      for ( int each = 0; each < input; ++each) {
      printf ( "value[%d] %ldn", each, value[each]);
      }

      return 0;
      }





      share|improve this answer


























        0












        0








        0







        Consider using fgets to read a line. Parse the line with sscanf. The %n specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.



        #include <stdio.h>
        #include <stdlib.h>

        #define SIZE 4000
        #define LIMIT 500

        int main( void) {
        char line[SIZE] = "";
        char bracket = 0;
        long int value[LIMIT] = { 0};
        int result = 0;
        int input = 0;
        int offset = 0;
        int span = 0;

        printf ( "enter values x:{w,y,...,z}n");
        if ( fgets ( line, sizeof line, stdin)) {
        //scan for long int, optional whitespace, a colon,
        //optional whitespace, a left brace and a long int
        if ( 2 == ( result = sscanf ( line, "%ld : {%ld%n", &value[0], &value[1], &offset))) {
        input = 1;
        do {
        input++;
        if ( LIMIT <= input) {
        break;
        }
        //scan for optional whitespace, a comma and a long int
        //the scan will fail when it gets to the right brace }
        result = sscanf ( line + offset, " ,%ld%n", &value[input], &span);
        offset += span;//accumulate processed characters
        } while ( 1 == result);
        //scan for optional space and character ie closing }
        sscanf ( line + offset, " %c", &bracket);
        if ( '}' != bracket) {
        input = 0;
        printf ( "line was not terminated with }n");
        }
        }
        }
        else {
        fprintf ( stderr, "fgets EOFn");
        return 0;
        }

        for ( int each = 0; each < input; ++each) {
        printf ( "value[%d] %ldn", each, value[each]);
        }

        return 0;
        }





        share|improve this answer













        Consider using fgets to read a line. Parse the line with sscanf. The %n specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.



        #include <stdio.h>
        #include <stdlib.h>

        #define SIZE 4000
        #define LIMIT 500

        int main( void) {
        char line[SIZE] = "";
        char bracket = 0;
        long int value[LIMIT] = { 0};
        int result = 0;
        int input = 0;
        int offset = 0;
        int span = 0;

        printf ( "enter values x:{w,y,...,z}n");
        if ( fgets ( line, sizeof line, stdin)) {
        //scan for long int, optional whitespace, a colon,
        //optional whitespace, a left brace and a long int
        if ( 2 == ( result = sscanf ( line, "%ld : {%ld%n", &value[0], &value[1], &offset))) {
        input = 1;
        do {
        input++;
        if ( LIMIT <= input) {
        break;
        }
        //scan for optional whitespace, a comma and a long int
        //the scan will fail when it gets to the right brace }
        result = sscanf ( line + offset, " ,%ld%n", &value[input], &span);
        offset += span;//accumulate processed characters
        } while ( 1 == result);
        //scan for optional space and character ie closing }
        sscanf ( line + offset, " %c", &bracket);
        if ( '}' != bracket) {
        input = 0;
        printf ( "line was not terminated with }n");
        }
        }
        }
        else {
        fprintf ( stderr, "fgets EOFn");
        return 0;
        }

        for ( int each = 0; each < input; ++each) {
        printf ( "value[%d] %ldn", each, value[each]);
        }

        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 18:43









        xingxing

        1,339288




        1,339288
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459142%2fc-filling-array-from-specific-single-line-input%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin