So, I'm having a problem with my code. I was trying to use fgets() for strings and scanf() for ints, but I read that this is bad practice and conflictive. I then tried using fgets() + sscanf() to read and parse a string to int. Still, for the love of me, I can't figure out what's happening! This is the code:
void menuUtente(CentroVacinacao *c)
{
Utente u;
char temp[20];
printf("Introduza o nome do utente: \n");
fgets(u.nomeUtente, NOME, stdin);
while (getchar() != '\n')
{
}
u.nomeUtente[strlen(u.nomeUtente) - 1] = '\0';
printf("Introduza o número do utente: \n");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.numeroUtente);
printf("Introduza a idade do utente: \n");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.idade);
printf("Introduza o contacto do utente: \n");
fgets(u.contacto, 9, stdin);
u.contacto[strlen(u.contacto) - 1] = '\0';
printf("Introduza a vacina tomada: \n");
fgets(u.vacinaTomada.designacaoVacina, DESIGNACAO, stdin);
u.vacinaTomada.designacaoVacina[strlen(u.vacinaTomada.designacaoVacina) - 1] = '\0';
printf("Introduza as doses administradas: \n");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.dosesAdministradas);
printf("Introduza a data da última dose (DD/MM/AAAA): \n");
fgets(u.dataUltimaDose, 10, stdin);
u.dataUltimaDose[strlen(u.dataUltimaDose) - 1] = '\0';
acrescentaUtente(c, u);
}
Can someone tell me what's happening? It's literally ignoring my first input and putting to inputs in the same line. Thanks in advance, been trying to figure this out for a while.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct sVaccine
{
int vacID;
char vacName[25];
int numDoses;
int monthsBetweenDoses;
} Vaccine;
typedef struct sPatient
{
int patientNumber;
char patientName[100];
int age;
char phoneNo[11];
Vaccine vacTaken;
int doses;
char lastDoseDate[10];
} Patient;
typedef struct sVaccinationCenter
{
int centerCode;
char centerName[100];
char address[20];
Patient vaccinatedPatient[3000];
Vaccine vaccineUsed;
int numberOfPatients;
} VaccinationCenter;
VaccinationCenter init()
{
VaccinationCenter c;
c.numberOfPatients = 0;
return c;
}
int addPatient(VaccinationCenter *c, Patient u)
{
if (c->numberOfPatients == 3000)
{
return -1; // O centro está cheio
}
c->vaccinatedPatient[c->numberOfPatients] = u;
c->numberOfPatients++;
return 0;
}
void menuPatient(VaccinationCenter *c)
{
Patient u;
char temp[20];
printf("Patient Name: ");
fgets(u.patientName, 100, stdin);
u.patientName[strlen(u.patientName) - 1] = '\0';
printf("Patient Number: ");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.patientNumber);
printf("Patient Age: ");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.age);
printf("Patient Contact: ");
fgets(u.phoneNo, 11, stdin);
u.phoneNo[strlen(u.phoneNo) - 1] = '\0';
printf("Vaccine Used: ");
fgets(u.vacTaken.vacName, 25, stdin);
u.vacTaken.vacName[strlen(u.vacTaken.vacName) - 1] = '\0';
printf("Doses Taken: ");
fgets(temp, sizeof(temp) - 1, stdin);
sscanf(temp, "%d", &u.doses);
printf("Last Dose Taken (DDMMAAAA): ");
fgets(u.lastDoseDate, 10, stdin);
u.lastDoseDate[strlen(u.lastDoseDate) - 1] = '\0';
addPatient(c, u);
}
void listCenter(VaccinationCenter c)
{
for (int i = 0; i < c.numberOfPatients; i++)
{
printf("Patient Name: %s\n", c.vaccinatedPatient[i].patientName);
printf("Patient Number: %d\n", c.vaccinatedPatient[i].patientNumber);
printf("Patient Age: %d\n", c.vaccinatedPatient[i].age);
printf("Patient Contact: %s\n", c.vaccinatedPatient[i].phoneNo);
printf("Vaccine Used: %s\n", c.vaccinatedPatient[i].vacTaken.vacName);
printf("Doses Taken: %d\n", c.vaccinatedPatient[i].doses);
printf("Last Dose Taken: %s\n", c.vaccinatedPatient[i].lastDoseDate);
}
}
int main(int argc, char const *argv[])
{
VaccinationCenter c = init();
int opcao = -1;
//menuUtente(&c);
c.numberOfPatients = 0;
while (opcao != 0)
{
printf("1 - Insert patient;\n");
printf("2 - List patients;\n");
printf("0 - Leave;\n");
printf("Enter your choice -> ");
scanf("%d", &opcao);
switch (opcao)
{
case 1:
menuPatient(&c);
break;
case 2:
listCenter(c);
break;
case 0:
break;
default:
printf("Invalid Option!\n");
break;
}
}
return 0;
}