Home > OS >  Issue printing strings from struct in c
Issue printing strings from struct in c

Time:10-15

I am trying to write a program that takes first names and last names from stdin separated by newline and stores them in char arrays inside of a struct array. Everything seems to be working fine until I want to print them in a certain way. I need the names to be formatted like this on the output: <lastName>, <firstName> That is why I added a function to remove the trailing newline from the end of each line when parsing the values. It however does not return anything when doing this:

printf("%s, %s",people[0].lastName, people[0].firstName);

However when printing the values on seperate lines like

printf("%s\n",people[0].lastName);
printf("%s\n",people[0].firstName);

or using puts

puts(people[0].lastName);

it works just fine. When I comment out the removeNewline() function it seems to get rid of the problem. But with the newline at the end I cannot format the output the way I desribed. Here is my full code:

#include <stdio.h>
#include <string.h>

#define MAX_STRING_LENGTH 100
#define MAX_PEOPLE 42

struct Person{
  char firstName [MAX_STRING_LENGTH];
  char lastName[MAX_STRING_LENGTH];
};

void removeNewline(char * string){
  for(int i = 0; string[i] != '\0'; i  ){
    if(string[i] == '\n'){
      string[i] = '\0';
    }
  }
}

int parseData(struct Person * people){
    int peopleCounter;
    for(peopleCounter = 0; peopleCounter < MAX_PEOPLE; peopleCounter  ){
        char firstName[MAX_STRING_LENGTH];
        char lastName[MAX_STRING_LENGTH];

      if(fgets(firstName,MAX_STRING_LENGTH,stdin) == NULL){
        break;
      }
      fgets(lastName,MAX_STRING_LENGTH,stdin);
        
      removeNewline(firstName);
      removeNewline(lastName);

      strcpy(people[peopleCounter].firstName,firstName);
      strcpy(people[peopleCounter].lastName,lastName);
    }
    return peopleCounter;
}

int main(int argc, char *argv[]){

    struct Person people [MAX_PEOPLE];
    parseData(people);

    //PROBLEM
    printf("%s, %s",people[0].firstName, people[0].lastName);

    for(int i = 0; i < 2; i  ){
        printperson(people, i);
    }
}

I am giving the values from a text file through redirection like so:

./main <people.txt
people.txt example:
George
Washington
Thomas
Jefferson
desired output:
Washington, George
Jefferson, Thomas

CodePudding user response:

I am not able to reproduce the issue. Here are the minor changes:

  1. removeNewline(): return after we process the first newline. Streamlined it a bit.
  2. parseData(): store directly in struct instead of temporary variables. Renamed peopleCounter to i as it was it unnecessary long. Check return value of 2nd fgets(), too.
  3. main(): Removed call to printperson() and looped over the returned values with the two arguments variables switched in the printf() statement for the expected order.
#include <stdio.h>
#include <string.h>

#define MAX_STRING_LENGTH 100
#define MAX_PEOPLE 42

struct Person{
    char firstName[MAX_STRING_LENGTH];
    char lastName[MAX_STRING_LENGTH];
};

void removeNewline(char *s) {
    for(;;) {
        *s *= (*s != '\n');
        if(!*s  ) return;
    }
}

int parseData(struct Person *people){
    int i = 0;
    for(; i < MAX_PEOPLE; i  ){
        if(!fgets((people   i)->firstName,MAX_STRING_LENGTH,stdin))
            break;
        removeNewline((people   i)->firstName);
        if(!fgets((people   i)->lastName,MAX_STRING_LENGTH,stdin))
            break;
        removeNewline((people   i)->lastName);
    }
    return i;
}

int main(int argc, char *argv[]) {
    struct Person people[MAX_PEOPLE];
    int n = parseData(people);
    for(int i = 0; i < n; i  ) {
        printf("%s, %s\n", people[i].lastName, people[i].firstName);
    }
}

and the output is:

Washington, George
Jefferson, Thomas
  • Related