1

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?

3
  • I can barely read your output. A cropped bigger picture would be so much better than this :-) Commented Mar 2, 2013 at 4:25
  • 1
    @Aniket: with most current browsers, you should be able to right-click on the picture and open it in a new tab (or new window), which you can then view full-size. Commented Mar 2, 2013 at 4:28
  • Count the letters in your surnames and match that with the column offsets, you'll see what's up with the alignment. You want a single width limit for the combined name, that will mean building the combination separately. Commented Mar 2, 2013 at 4:31

3 Answers 3

4

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.

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

3 Comments

Yeah I am kinda stumped reading through my code now as to how it got there, will make an edit about it if I dont see it after a few more sweeps! Also! Thanks!
There anyway to get rid of the whitespaces? I have everything lined up but the commas are all far over to the right from the last name but lined up
Lol wow what a smart workaround, didnt think of that, thank you!
1

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.

Comments

1

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

1 Comment

now just give the maximum length of your first name and last name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.