这是我进入C类的,我无法弄清楚为什么我会收到这个错误:
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);
}
}
代码:
{{1}}
答案 0 :(得分:2)
报告的错误来自您的行
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
将ph[i].vi.temperature
(在本例中为float
)的任何内容转换为int
,而scanf
需要指向int
的指针}。
现在,在您的情况下,您似乎需要温度为int
,而
ph[i].vi.temperature
持有float
,所以你宁愿使用另一个int
变量,比如说
int itemp;
scanf ("%d", &itemp);
输入然后
ph[i].vi.temperature = (float) itemp;
用于铸造。
或者,您可以简单地scanf ("%f", &ph[i].vi.temperature);
然后保留不可或缺的部分。
我不知道你的需求,也不知道代码背后的逻辑。
注意:我不确定您是否以符合您需求的方式使用scanf
的返回值。
在您的情况下,scanf
可以返回0
,1
或EOF
。