[英]Hi,I need to ask the user to enter 3 values until he enters EOF(Ctrl+z).The EOF part isn't working and the user enters 6 values each time and I need 3
#define SIZE 7
#include<stdio.h>
int main()
{
int NoOfDoors[SIZE];
int CarManufYear[SIZE];
float CarCost[SIZE];
for(int i=0; i<SIZE; i++)
{
printf("\nEnter 3 values (Door, year, price):");
scanf("%d%d%f", &NoOfDoors[i], &CarManufYear[i], &CarCost[i]);
if(scanf("%d%d%f", &NoOfDoors[i], &CarManufYear[i], &CarCost[i])==EOF)
{
break;
}
}
printf("End of the code");
return 0;
}
// the code is reading 6 values (instead of 3) then print the message again to read. // 代码正在读取 6 个值(而不是 3 个),然后再次打印消息以进行读取。 Also, the EOF part is not working properly.此外,EOF 部分无法正常工作。
Call scanf()
once per iteration and save the return value.每次迭代调用scanf()
一次并保存返回值。 Then test the return value.然后测试返回值。
for (int i=0; i<SIZE; i++) {
printf("\nEnter 3 values (Door, year, price): ");
int retval = scanf("%d%d%f", &NoOfDoors[i], &CarManufYear[i], &CarCost[i]);
if(retval == EOF) {
break;
}
if(retval != 3) {
Handle_incorrect_input(); // TBD code
break;
}
printf("Door:%d Year:%d Price:%.2f", NoOfDoors[i], CarManufYear[i], CarCost[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.