gpt4 book ai didi

C while循环无限循环EOF不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:54 25 4
gpt4 key购买 nike

我的 while 循环中的 EOF 有问题。它似乎并没有在输入 EOF 时简单地结束,而是这样做......

我该如何修复它并让 while 循环停止并继续运行。谢谢。

    #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>

void findLargestandSmallest(int integer, int* largest, int* smallest, int* average, int count);

int main(void)
{
//Local Declaration
int integer;
int largest;
int smallest;
int average;
int count; // number count

//Starting statemnets
smallest = INT_MAX;
largest = INT_MIN;
count = 0;

// Starting prompts
printf("\nHello this program will take in intagers and print");
printf("\nout the largest, smallest and avarage of integers");
printf("\nenterd int.");
printf("\nPlease enter in a integer ");

while (scanf("%d", &integer) != EOF)
{
if (integer != EOF)
{
count++;
findLargestandSmallest(integer, &largest, &smallest, &average, count);
}
else
{
printf("\n \n");
}
}

printf("\nThe largest number entered is %d and the smallest", largest);
printf("\nwas %d and the average of all the numbers is %d\n", smallest, average);
return 0;
}

void findLargestandSmallest(int integer, int *largest, int *smallest, int *average, int count)
{
int x; // just a holder variable

// finds average
x = 0;
x += integer;
*average = (x / count);

// Finds smallest and largest
if (integer <= *smallest)
{
*smallest = integer;
}
if (integer >= *largest)
{
*largest = integer;
}
printf("Enter another integer or <EOF> to quit ");
return;
}


[1]: http://i.stack.imgur.com/P0307.png

更新:我发现我做错了什么。这很简单。在 while 循环中 while(scanf("%d", &integer) != EOF) 不要那样设置,而是这样 (scanf("%d", &integer)) 理解 EOF。要在 DOS 中简单地调用它,请在最后一次输入时使用“Ctrl+Z”。即“number^Z”是使用“Ctrl+Z”后的样子 对于遇到此问题的任何其他人,这里还有针对此问题的更好且有效的代码。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>

void findLargestandSmallest(int integer, int* largest, int* smallest);

int main(void)
{
//Local Declaration
int integer;
int largest;
int smallest;
int average;
int sum;
int count;

//Starting statemnets
smallest = INT_MAX;
largest = INT_MIN;
count = 0;
sum = 0;

// Starting prompts
printf("\n--------------------------------------------------------");
printf("\n- Hello, this program will take in intagers and print -");
printf("\n- out the largest, smallest, and avarage of the -");
printf("\n- integers enterd. -");
printf("\n- NOTE: To quit: use \"Ctrl+Z\" on the last integer -");
printf("\n- you enter i.e \"number^z\" -");
printf("\n--------------------------------------------------------\n");
printf("\nEnter integers\n");

// Finds largest and smallest number
while (scanf("%d", &integer))
{
sum += integer;
count++;
findLargestandSmallest(integer, &largest, &smallest);
}

// Finds average
count--;
average = (sum / count);

// End prompts
printf("\n--------------------------------------------------------");
printf("\nThe largest number entered was %d, the smallest", largest);
printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
printf("\n--------------------------------------------------------");
printf("\nGoodbye\n");
return 0;
}

void findLargestandSmallest(int integer, int *largest, int *smallest)
{
if (integer < *smallest)
{
*smallest = integer;
}
if (integer > *largest)
{
*largest = integer;
}
return;
}

最佳答案

scanf 返回成功转换的元素数。如果它无法转换任何内容,则返回 0。EOF 仅在文件结束时返回(在 Unix 上为 Control-D)。

因此您应该修改程序以保存 scanf 的返回值,然后分别测试 0 和 EOF

integer 变量与 EOF 进行比较也是毫无意义的,因为关于 EOF 你可能知道的只是它是一个负数整数。阅读 scanf 手册页并了解它的作用以及它在何时何地返回的内容。这将解决这个难题。 :-)

好的,还有一些提示。你能理解这一点吗?

for (;;) {
int successfully_converted = scanf("%d", &integer);

if (successfully_converted == EOF) {
/* Do something when the user is tired of typing. */
puts("Thank you for an enjoyable game.\n");
exit(0);
} else if (successfully_converted == 0) {
puts("This didn't look like an integer\n");
exit(1);
} else {
/* Do something with integer. */
}
}

关于C while循环无限循环EOF不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22203911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com