0

I found this exercise where I had to create a data structures, read some inputs, store the inputs on the "appropriate" data structure and then print everything to a text file.

I am looking forward to adding a function to order the hotel list according to the stars number, so if an hotel has 5 stars, it will be at the top of the list. Is it possible to do that? Can I do that with arrays?

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

struct inputs {
   char nome[30];
   char via[30]; 
   char stars[30];
   int num;
};

int main(void)
{  

   struct inputs inputs = {"", "", ""};

   FILE *cfPtr; // cfPtr = clients.txt file pointer   
   // fopen opens file. Exit program if unable to create file 
   if ((cfPtr = fopen("clients.txt", "w")) == NULL) {
      puts("File could not be opened");
   } 
   else { 
      puts("Enter the account, name, and balance.");
      puts("Enter EOF to end input.");
      printf("%s", "? ");


      //scanf("%[^\n]s%[^\n]s%[^\n]s", inputs.via, inputs.nome, inputs.stars);



      fgets(inputs.nome, 30, stdin);
      inputs.nome[strcspn(inputs.nome,"\n")] = '\0';
      fgets(inputs.via, 30, stdin);
      inputs.via[strcspn(inputs.via,"\n")] = '\0';
      fgets(inputs.stars, 30, stdin);
      inputs.stars[strcspn(inputs.stars,"\n")] = '\0';

      inputs.num = strlen(inputs.stars);


      //printf("%s%s%s", inputs.nome, inputs.via, inputs.stars);

      // write account, name and balance into file with fprintf
      while (!feof(stdin)) {
           fprintf(cfPtr, "%s; %s; %s; %d\n", inputs.nome, inputs.via, inputs.stars, inputs.num);

           /*scanf("%[^\n]s", inputs.via);
           scanf("%[^\n]s", inputs.nome);
           scanf("%[^\n]s", inputs.stars);*/
           fgets(inputs.nome, 30, stdin);
           inputs.nome[strcspn(inputs.nome,"\n")] = '\0';
           fgets(inputs.via, 30, stdin);
           inputs.via[strcspn(inputs.via,"\n")] = '\0';
           fgets(inputs.stars, 30, stdin);
           inputs.stars[strcspn(inputs.stars,"\n")] = '\0';
           inputs.num = strlen(inputs.stars);

      }

      fclose(cfPtr); // fclose closes file   
   } 
} 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
All01
  • 61
  • 5

1 Answers1

0

I would suggest creating a struct type to represent one hotel, and then create an array of instances of the struct type. For instance the type could be as follows.

struct Hotel {
    uint8_t stars;
    char name[HOTELNAMESIZE];
};

struct Hotel hotels[NRHOTELS];

Now you can implement a sort function that sorts the hotels array based on hotels[i].stars. Of course this is a very simple implementation that has a hardcoded HOTELNAMESIZE and NRHOTELS value, but could be a decent starting point.

learnlearnlearn
  • 946
  • 1
  • 8
  • 16
  • So, in hotels[NRHOTELS]; each element will be a data-structures? For example hotels[NRHOTELS] = ["**** hotel1", "*** hotel two" .... ] ? – All01 Aug 26 '18 at 16:25
  • And how do i save a variable into the array? With the same system i'm using in my actual program? – All01 Aug 26 '18 at 17:08