[英]Fgets() and sscanf() in a menu
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.
我试图将 fgets() 用于字符串,将 scanf() 用于整数,但我读到这是不好的做法并且有冲突。 I then tried using fgets() + sscanf() to read and parse a string to int.
然后我尝试使用 fgets() + sscanf() 读取字符串并将其解析为 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);
}
And this is the output:这是 output:
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;
}
Try adding this getchar()
:尝试添加这个
getchar()
:
printf("Enter your choice -> ");
scanf("%d", &opcao);
getchar();
This seemed to make the second part of code you pasted work.这似乎使您粘贴的代码的第二部分起作用。 It doesn't skip anymore and it lists the registered patients.
它不再跳过,它列出了已注册的患者。 The thing is: I couldn't understand if thats the issue you are trying to solve.
问题是:我不明白这是否是您要解决的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.