我正在为一个学校项目编写程序,该程序可以计算房间的大小并计算油漆,人工等的价格。用户可以在此功能中输入房间的尺寸。变量似乎分配不正确。在主函数中,我将&
放在参数中的变量旁边,并在此函数中在此处添加了*
,但这并没有解决程序,最终导致错误提示:
[错误]对二进制<=的无效操作数(具有'float *'和'double')
float areaInput(float* height, float* width, float* length) /*recieve pointer value in c function*/
{
do{
printf("Please enter the height of the room in metres: ");
scanf("%f", &height);
emptyBuffer();
if (height <= 2.0 || height >= 4.6)
{
printf("Please enter a value between 2.1 - 4.5 metres\n");
}
}while(height <= 2.0 || height >= 4.6);
do{
printf("Please enter the width of the room in metres: ");
scanf("%f", &width);
emptyBuffer();
if (width <= 1.74 || width >= 8.21)
{
printf("Please enter a value between 1.75 - 8.2 metres\n");
}
}while(width <= 1.74 || width >= 8.21);
do{
printf("Please enter the length of the room in metres: ");
scanf("%f", &length);
emptyBuffer();
if (length <= 1.74 || length >= 8.21)
{
printf("Please enter a value between 1.75 - 8.2 metres\n");
}
}while(length <= 1.74 || length >= 8.21);
}
答案 0 :(得分:1)
由于参数是指针,因此您需要使用*
取消引用它们以访问值。您也不需要在调用&
时使用scanf()
,因为它期望指向存储输入的位置的指针,而这就是变量的含义。
除了在if
和do-while
中进行相同的范围测试之外,您还可以在break
失败时使用if
。
float areaInput(float* height, float* width, float* length) /*recieve pointer value in c function*/
{
while (1) {
printf("Please enter the height of the room in metres: ");
scanf("%f", height);
emptyBuffer();
if (*height <= 2.0 || *height >= 4.6) {
printf("Please enter a value between 2.1 - 4.5 metres\n");
} else {
break;
}
}
while (1) {
printf("Please enter the width of the room in metres: ");
scanf("%f", width);
emptyBuffer();
if (*width <= 1.74 || *width >= 8.21) {
printf("Please enter a value between 1.75 - 8.2 metres\n");
} else {
break;
}
}
while (1) {
printf("Please enter the length of the room in metres: ");
scanf("%f", length);
emptyBuffer();
if (*length <= 1.74 || *length >= 8.21) {
printf("Please enter a value between 1.75 - 8.2 metres\n");
} else {
break;
}
}
}
顺便说一句,您的输入验证消息与您正在测试的消息不匹配。如果用户输入高度2.05
,即使它不在2.1
和4.5
之间,也将允许它。您假设用户将只在小数点后输入1位数字,并且还没有考虑到浮点在许多小数点后都有错误的事实。例如,使用< 2.1
代替<= 2.0
。