简体   繁体   English

嗨,我需要让用户输入 3 个值,直到他输入 EOF(Ctrl+z)。EOF 部分不起作用,用户每次输入 6 个值,我需要 3 个

[英]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.

相关问题 为什么需要多个EOF(CTRL + Z)字符? - Why do I require multiple EOF (CTRL+Z) characters? 我如何才能从用户那里获得输入整数,直到他使用eof按下回车键? - How can I get input integers from user until he press enter by using eof? C prog - 需要在用户输入输入时检查字符串的一部分 - C prog - need to check a part of string while user enters input 为什么 EOF (ctrl+d) 在 function 代码中不执行? 而且只有 ctrl+z - Why doesn't EOF (ctrl+d) execute in function code? And only ctrl+z 我确保用户输入的数字是有效数字。 如果人们不输入数字,if 语句会以某种方式起作用吗? 这是如何工作的? - I’m making sure the user enters a number a valid number. If persons doesn’t enter a number, the if statement somehow works? How is this working? getchar()不会传递EOF且Ctrl + Z不会终止Cygwin上的程序 - getchar() doesn't pass EOF and Ctrl+Z doesn't terminate the program on Cygwin 循环直到用户输入'x' - looping until user enters 'x' 为什么我必须键入 CTRL + Z 3 次才能发送 EOF? - Why do I have to type CTRL + Z 3 times to send EOF? 我怎么知道用户输入的字符是否超过1个? 如果他输入多个字符,我想将其存储在字符数组中 - How can I know if user enters more than 1 character as input? And if he enters more than one character I want to store that in character array 在使用 scanf(&quot;%i&quot;,var) 时,如果用户输入一个字母或只是按 Enter 键,我会遇到问题 - While using scanf("%i",var) if the user enters a letter or just presses enter i get a problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM