0

I have given input which contains data that I am going to process and saved into an array. The input looks like this :

{ [1, 10], [2,1] , [-10, 20] }

it can have more elements in it. I need to process it that I can load all numbers from [ number , number ] into 2d array , first number should be at 0th and second number should be at 1st index so this array should look like

[[1,10],[2,1],[-10,20]]

But I've failed to find the solution, how to process this input into my desired array. What is the right way to do it? I tried to do as following:

   int main()
{
    long long int cisla[10][2];
    int x;
    int y;
    int i;
    int index=0;
    int counter=0;
    char c;
    char zatvorka_one;
    char zatvorka_three;
    char ciarka;
    char ciarka_two;
    printf("Pozicia nepriatela\n");
    c=getchar();
    if(c!='{'){
           return 0;
    }
    scanf(" %c%d,%d%c",&ciarka,&x,&y,&zatvorka_one);
    cisla[index][0]=x;
    cisla[index][1]=y;
    index++;
    while(1){
        scanf("%c",&ciarka);
        if(ciarka=='}'){
            break;
        }
       scanf(" %c%d%,%d%c",&ciarka,&x,&y,&zatvorka_one);
       cisla[index][0]=x;
       cisla[index][1]=y;
       index++;
    }
    for ( i = 0; i < index; i++){
    printf("%d %d\n",cisla[i][0],cisla[i][1]);
}
}

But somehow it returns unexpected result, how can i fix it?

5
  • 2
    scanf() will not work for this. Better read the whole line using fgets() and then process it. Commented Jan 13, 2016 at 9:28
  • how could i process is? Thats the main question , thats why i used scanf so bcs nothing came to my mind. Commented Jan 13, 2016 at 9:44
  • after you read the line in a char array, go through the characters one by one and store them in the array when you encounter a number. Commented Jan 13, 2016 at 9:49
  • are their multiple lines of input or only a single line? Do you want the saved array to contain text values or int values? Can you assume the input is formatted correctly? Commented Jan 15, 2016 at 18:48
  • you could use getline() or readline() to input the whole line at once. Then implement a state machine that has states like: find{ find[ findint find, find] and find} extractint then link those states together with appropriate state transition code. the code would scan the line, char by char. be sure to handle errors, like when the expected next state is not foundThe rest should be easy. Commented Jan 15, 2016 at 18:56

2 Answers 2

1

You should use gets instead of scanf. gets will return the entire string wich will be easear. Then you ahould read about strtok wich can be used to separate a string. For example: strtok(s,",") will separate your string into smaller strings. For the input {[12,4], [8,9]} will divide into: first string: {[12 second string: 4] third string [8 and fourth string 9]}. Now you will just have to remove the characters that are not numbers like { } and []. After that you will have strings only with the numbers so you can use another predefined fuction you should read abput called atoi. It recieves a string and turns it into an int (ascci to int). There is also an atof (ascci to float) if you need it. Tuturialpoints is a good place to look for examples on how to use these functions i mentioned.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm relatively new to C and SO. Maybe I shouldn't give you the solution, but I did. It follows the advice of sharp c student.

You could try to do it like this:

#include "stdafx.h"
#include "string.h"
#include "stdlib.h"

#define MAXNBRELM 10    //  maximum number of elements; adjust as needed

int main()
{
    int     IntArr[MAXNBRELM][2];   //  integer array to hold results
    int     s = 0;                  //  subscript
    int     NbrElm;                 //  number of elements found
    char    Buf[81];                //  buffer to hold input
    char    * StrPtr;               //  pointer to string for fgets         
    char    * TknChr;               //  each individual token
    char    * NxtTkn;               //  next token position (only needed for Visual C++)

    StrPtr = fgets(Buf, 80, stdin);

    TknChr = strtok_s(Buf, " {[,]}", &NxtTkn);
    while (s <= MAXNBRELM && TknChr != NULL) {
        IntArr[s][0] = atoi(TknChr);
        TknChr = strtok_s(NULL, " {[,]}", &NxtTkn);
        if (TknChr != NULL) {
            IntArr[s][1] = atoi(TknChr);
            TknChr = strtok_s(NULL, " {[,]}", &NxtTkn);
            s++;
        }
    }

    NbrElm = s;
    for (s = 0; s < NbrElm; s++)
        printf("%d %d\n", IntArr[s][0], IntArr[s][1]);

    return 0;
}

This is for Visual Studio, is why I needed to use strtok_s and &NxtTkn.

4 Comments

How do you know they are using Visual Studio?
I don't understand the down vote on the question. I actually don't think they are using Visual Studio, but that's what I have to test the solution. There are relatively few changes to make to use in some other environment. Like the quotes around the include files may need to replaced by angle brackets. The "strtok_s" needs to be replaced with "strtok" and the &NxtTok variable needs to be removed. I don't know other environments to say for certain these are the only changes, but it should be close enough for government work.
So then why not use standard C?
I don't have a standard C compiler, just a Visual Studio compiler. That's what I used to test the solution. So I don't understand the down vote on the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.