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
}
}