Sherifftwinkie
Sherifftwinkie

Reputation: 391

Formatting printf and extra string print?

I have 2 issues, the first one is, I can't get my lines to match up in the print out of my array. I can only get the first line to match up!

void printStructs(struct student *list, int size){

int j = 0;

printf("Last Name, First Name            Term      ID      Course   Section\n");
printf("-------------------------------- ----  ---------   ------   -------\n");

while(j < size){

    printf("%s, %-21s %-5d %-11d %s%d %7s\n", list[j].lastname, list[j].firstname, list[j].term, list[j].id, list[j].subjectname,list[j].catalog, list[j].section);

    j++;

}
}

This provides me with the output of this!

Detail

See how only the first one is lined up properly? How do I get the remaining lines to do this!?

Now my second issue is one I just simply can't see. Notice that extra "Susan" in the second to last line before the last name is displayed? Where is that coming from!? It works for all the other names, what am I not seeing here?

EDIT: The error involving the extra name CAN NOT BE FOUND HERE! just realized it has something to do with my loop initializing the name to that value, will edit further if I can't figure it out, for now ignore!

EDIT2 "Extra String Issue!":

Alright so I have narrowed down where the issue is but not exactly why it happens, I believe it has something to do with delimiters...here is the code

studentsRead = readStudentList(inputf, students, N);

printf("%s\n", students[6].lastname);

printStructs(students, studentsRead);

/*for (j = 1 ; j <= 10-1 ; j++){

    for(k = 0 ; k <= 10-2 ; k++){

        if(students[k] > students[k+1]){

            temp = students[k];
            students[k] = students[k+1];
            students[k+1] = temp;

            }
        }
    }*/

fclose(inputf);

return 0;

 }

int readStudentList(FILE *f, struct student *list, int maxSize){

char buff[65];
int count = 0;

while((count < maxSize) && (fgets(buff, sizeof(buff), f) != NULL)){

    struct student item;

    if(parseStudent(&item, buff)){

        list[count++] = item;

    }
}

return count;

}

int parseStudent(struct student *person, char *data){

char *ptr;

int i = 0;

ptr = strtok(data, DELIM);

person[i].term = atoi(ptr);

ptr = strtok(NULL, DELIM);

person[i].id = atoi(ptr);

ptr = strtok(NULL, DELIM);

strcpy(person[i].lastname, ptr);

printf("%s\n", person[i].lastname);

ptr = strtok(NULL, DELIM);

strcpy(person[i].firstname, ptr);

ptr = strtok(NULL, DELIM);

strcpy(person[i].subjectname, ptr);

ptr = strtok(NULL, DELIM);

person[i].catalog = atoi(ptr);

ptr = strtok(NULL, DELIM);

strcpy(person[i].section, ptr);

}

Going through this code everything prints normally until that printf after my readStudentList call, which prints out the last name with the first name String stuck in at the end. Although printed in my parse function the last name seems to appear normally? I've been attempting to put print outs all over the place to see where it goes wrong but some of them wont even print anything?

Upvotes: 1

Views: 772

Answers (3)

Bhavesh Jethani
Bhavesh Jethani

Reputation: 3875

printf ("%d %.s", number, SIZE, letters);
Note: there is a distinction between width (which is a minimum field width) and precision (which gives the maximum number of characters to be printed). %s specifies the width, %.s specifies the precision. (and you can also use %.
but then you need two parameters, one for the width one for the precision)

enter image description here

Upvotes: 1

Adeel Ahmed
Adeel Ahmed

Reputation: 1601

The problem is in %s of printf, you have to use single format specifier for last name and first name. So concatenate last name and first name in separate buffer and used that buffer in printf.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490663

The problem is where you're printing the last name with just %s instead of specifying a width. That means the rest of the line is getting shifted left or right depending on the length of the first name.

As for how you should format this, you probably want to start by concatenating the first and last names together, then printing the result in a fixed-width:

char buffer[256];

sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);

printf("%-32s ...", buffer, ...);

My guess is that the duplicated name problem stems from reading the data incorrectly; doesn't seem to be in the code you've shown.

Upvotes: 4

Related Questions