Home > other >  How do I assign an array of structs using for loop to be printed out?
How do I assign an array of structs using for loop to be printed out?

Time:03-13

I am new to coding and still learning, so I would like to thank you for all the feed back that you give, Thank you. Currently I have a code that is able to read in all the names of the customers that are stored in the file. However it is not able to read in all the produce items and their amount other then the first three.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#define STRSIZE 31
#define INVENSIZE 31

typedef struct{
  char fname[STRSIZE];
  char lname[STRSIZE];
} fullname;
typedef struct{
  int count;
  char pname[STRSIZE];
} koala;
typedef struct{
  koala order[INVENSIZE];
  fullname comname[STRSIZE];
} consumers;


void ReadInCustomer (consumers *c){
  //reading in file and cheking if it exist
  FILE * CinFile;
  if ((CinFile =fopen("customers.txt","r")) == NULL){
    printf("ERROR: WRONG FILE");
  }
  else{
    //printf("I was able to read in customer file!!\n");
  }
  //assigning file elements into struct
  int i;
  int j;
  for(i =0; i < 12; i  ){
    fscanf(CinFile,"%s %s", c->comname[i].fname, c->comname[i].lname);
      for (j=0; j < 3; j  ){
      fscanf(CinFile, "%s %d", c->order[j].pname, &c->order[j].count);
    }
  }
  fclose(CinFile);
}


int main(){
  consumers con;
  ReadInCustomer (&con);
  int i;
  int j;    
  for(i =0; i < 12; i  ){
    printf("%s %s\n", con.comname[i].fname, con.comname[i].lname);
    for (j=0; j < 3; j  ){
      printf(" s: %d\n", con.order[j].pname, con.order[j].count);
      
    }
  }//printing out all of the customer and their orders
}

the file that I am reading in looks like this where it is for now repeated 12 times. where each element is all different. The sample below is just for example.

Firstname1 lastname1
item1 001
item2 010
item3 100
Firstname2 lastname2
item4 001
item5 010
item6 100
...

CodePudding user response:

If you expect to be able to store multiple koalas per consumers, this will not do it. How this would typically be accomplished in this context would be to declare the structure like this:

#include <stdio.h>
#include <stdlib.h>

#define MAX_CONSUMERS 10
#define STRSIZE 31
#define INVENSIZE 31

typedef struct{
  char fname[STRSIZE];
  char lname[STRSIZE];
} fullname;

typedef struct{
  int count;
  char pname[STRSIZE];
} koala;

typedef struct{
  koala order[INVENSIZE];
  fullname comname;
} consumer;

void ReadInCustomer (consumer *c)
{
    // Opening in-file and reading it
    const char *inFile = "customers.txt";
    FILE *inFp = fopen(inFile, "r");
    if (!inFp) {
        perror(inFile);
        exit(1);
    }
    // else printf("I was able to open the customer file!!\n");
    // assigning file elements into struct
    for (int i = 0; i < 12; i  ) {
        fscanf(inFp,"%s %s", c[i].comname.fname, c[i].comname.lname);
        for (int j = 0; j < 3; j  ) {
            fscanf(inFp, "%s %d", c[i].order[j].pname, &c[i].order[j].count);
        }
    }
    fclose(inFp);
}

int main(void) {
    consumer con[MAX_CONSUMERS];
    ReadInCustomer(con);
    for (int i = 0; i < 12; i  ) {
        printf("%s %s\n", con[i].comname.fname, con[i].comname.lname);
        for (int j = 0; j < 3; j  )
            printf(" s: %d\n", con[i].order[j].pname, con[i].order[j].count);
    }//printing out all of the customers and their orders
}

CodePudding user response:

Your program is writing to only the first 3 index of array koala order[INVENSIZE]; of structure consumers. The value you get for count and pname will be the last read values. Check this part of code of ReadInCustomer() function:

.....
      for (j=0; j < 3; j  ){
      fscanf(CinFile, "%s %d", c->order[j].pname, &c->order[j].count);
.....

For every customer, the items read are stored in first 3 indexes of order array member of structure consumers.

The way you have defined the consumers structure, using j as index will not work. Instead of using value of j as index, you should use (i * 3) j as index while filling order array because every consumer has 3 items listed under their name in the file. It should be:

.....
      for (j=0; j < 3; j  ){
      fscanf(CinFile, "%s %d", c->order[(i * 3)   j].pname, &c->order[(i * 3)   j].count);
.....

Similar changes while printing the values (in main()):

.....
    for (j=0; j < 3; j  ){
      printf(" s: %d\n", con.order[(i * 3)   j].pname, con.order[(i * 3)   j].count);
.....

Another minor problem in your code is - if due to some reason, fopen fails, your program will print a message to console and then attempt to read a NULL pointer. You should return in case of fopen() failure :

  if ((CinFile =fopen("customers.txt","r")) == NULL){
    printf("ERROR: WRONG FILE");
    return;     // This is missing in your code
  }

With this also, there would be a problem - the main() function which is calling ReadInCustomer() function will not come to know whether the reading of file was successful or any error occurred. Irrespective of whether file read successfully or not, the main() function will attempt to print the values of consumers structure members.

It would be better to change the return type of ReadInCustomer() function from void to int and return, say, -1 in case of failure. Check this return value of ReadInCustomer() function in the calling function and based on this return value take the next decision.

  • Related