【发布时间】:2018-03-27 00:18:48
【问题描述】:
这是我的 C 类课程,我不知道为什么会出现此错误:
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int'
代码:
struct vitalInformation {
float temperature;
unsigned int systolicPressure;
unsigned int diastolicPressure;
};
struct activityInformation {
unsigned int stepCount;
unsigned int sleepHours;
};
union patientHealth{
struct vitalInformation vi;
struct activityInformation ai;
} ph[100];
int i = 0;
int menu(){
int option;
printf ("Please enter the number for the desired action (1, 2, 3):\n");
printf ("1 - Enter some patient vital information\n");
printf ("2 - Enter some patient activity information\n");
printf ("3 - Print summary information on the patient information and exit the program\n");
scanf ("%d", &option);
while (scanf("%d", &option) || option<1 || option>3) {
printf ("Please enter 1, 2, or 3\n\n");
printf ("Please enter the number for the desired action (1, 2, 3):\n");
printf ("1 - Enter some patient vital information\n");
printf ("2 - Enter some patient activity information\n)");
printf ("3 - Print summary information on the patient information and exit the program\n");
fflush (stdin);
scanf ("%d", &option);
}
return option;
}
void patientVitalInfo(int *countP, float *minT, float *maxT, int *minS, int *maxS, int *minD, int *maxD) {
printf ("Enter the temperature: ");
scanf ("%f", &ph[i].vi.temperature);
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
printf ("Please enter an integral unsigned number\n");
printf ("Enter the temperature: ");
fflush (stdin);
scanf ("%f", &ph[i].vi.temperature);
}
}
【问题讨论】:
-
scanf需要指针作为参数,因为它正在存储值。您没有传递与错误消息完全相同的指针。您似乎已经正确完成了两次,一次错误,但我不确定您为什么要读取至少 2 个值。似乎是复制/粘贴和逻辑错误。 -
请注意
ph[i].vi.temperature是float。 -
代码后面有
scanf ("%f", &ph[i].vi.temperature);,为什么不做同样的事情而不是scanf ("%d", (int)ph[i].vi.temperature)? -
@DavidC.Rankin:
fflush对于输入流的行为是为许多在这里提问的人定义的。它不是由 C 标准定义的,而是 defined by Microsoft:“如果流对输入打开,则 fflush 会清除缓冲区的内容。” -
@DavidC.Rankin:我不是在告诉人们使用它,也不建议这样做。我认识到使用它的初学者有这样做的理由,并建议他们从代码中删除它会在他们所处的情况下导致问题。使用非可移植行为有有的原因.当有人致电
pthread_create时,您是否警告这是不可移植的行为,所以他们应该删除它?当有人致电CreateWindow时,您是否警告这是不可移植的行为?fflush(stdin)与那些没有什么不同。它是 C 标准欢迎的扩展。
标签: c