Kim Roberts
Kim Roberts

Reputation: 1

Scanning Strings on Multiple input lines - C language

I've got the following input:

10/9/02 9:15:59 23.845089 38.018470 DXUYHu

10/9/02 9:16:29 23.845179 38.018069 tKoPTx

10/9/02 9:16:59 23.845530 38.018241 JPQbNb

10/9/02 9:17:29 23.845499 38.017440 aEWdXS

10/9/02 9:17:59 23.844780 38.015609 gqeEjx

10/9/02 9:18:29 23.844780 38.014018 aQArkX

10/9/02 9:18:59 23.844869 38.012569 fhQIAS

10/9/02 9:19:29 23.845360 38.011600 BhngfQ

10/9/02 9:19:59 23.845550 38.010650 rgwehm

10/9/02 9:20:29 23.845100 38.010478 jdBgpN

and I am trying to develop a code which reads the final 6 character - the string for each line and thus prints it out. For some reason the code I've developed only prints out the final string 'jdBgpN' please help as to why.

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id);`
}

for (i=0; i < LASTROW; i++) {
    scanf("%s", id[i]);
}

for (i=0; i < LASTROW; i++) {
    printf("%s\n", id[i]);
}

Upvotes: 0

Views: 52

Answers (1)

djna
djna

Reputation: 55937

In the first for loop you overwrite your id, so you only keep one value

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id);`
}

Assuming that id is an array, then the array variable can be treated as a pointer and you can add the index to the variable to move along the arrray.

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id + i);`
}

Upvotes: 1

Related Questions