简体   繁体   English

如何在C语言中退出while循环?

[英]how to exit while loop in C?

#include<stdio.h>
#define LINESIZE 1024

int n, sum =0;
char line[LINESIZE];

int main() {
    while(1) {
        printf("enter an integer: ");
        if(!fgets(line, LINESIZE, stdin)) {
            clearerr(stdin);
            break;
        }

        if (sscanf(line, "%d", &n) == 1)
            sum += n;
    }
    printf("%d \n",sum);
}

When I run this in Cygwin, the output seems infinite and I don't know how to return sum ? 当我在Cygwin中运行此命令时,输出似乎是无限的,并且我不知道如何返回sum Am I missing something? 我想念什么吗?

enter an integer: 1
enter an integer: 2
enter an integer: 3
enter an integer: 4
enter an integer: 5
enter an integer: 6

Your while loop is fine, the program loops until it hits the end of file for stdin . 您的while循环很好,程序将循环直到到达stdin的文件末尾。 From the terminal, you can signal an end of file by pressing Ctrl-D under Unix and Ctrl-Z Enter on Windows. 在终端上,您可以通过按Unix下的Ctrl-D和Windows上的Ctrl-Z Enter来指示文件结束。

Alternately, you could exit the loop when you read some specific input, such as a blank line, a line without a number, a line with the word quit ... 或者,当您读取某些特定的输入时,例如空行,不带数字的行,带有单词quit的行,您可以退出循环。

Some remarks about the program: 关于程序的一些说明:

  • There is no reason to make your variables global, nor to clear the error condition on stdin . 没有理由将变量设为全局变量,也没有理由清除stdin上的错误情况。
  • The idiomatic statement for an infinite loop is for (;;) { ... } . 无限循环的惯用语是for (;;) { ... }
  • main() should return 0. main()应该返回0。

Here is a modified version of your program: 这是程序的修改版本:

#include <stdio.h>

#define LINESIZE 1024

int main(void) {
    char line[LINESIZE];
    int n, sum = 0;

    for (;;) {
        printf("enter an integer: ");
        if (!fgets(line, sizeof line, stdin)) {
            break;
        }
        if (*line == '\n') {
            /* stop on empty line */
            break;
        }
        if (!strcmp(line, "quit\n")) {
            /* stop if the user types quit */
            break;
        }
        if (sscanf(line, "%d", &n) == 1) {
            sum += n;
        }
    }
    printf("%d\n", sum);
    return 0;
}

In addition to the comments mentioned in the question, I would like to suggest a slightly different approach. 除了问题中提到的评论外,我想提出一种略有不同的方法。 You can modify the if statement which calculates the sum such that when the input from the user is not an int eger, you choose to exit the while loop. 您可以修改计算sumif语句,以便当来自用户的输入不是int时,您选择退出while循环。

    if (sscanf(line, "%d", &n) == 1) {
        sum += n;
    }
    else {
        printf("Could not get integer\n");
        break;
    }

Sample output 1: 样本输出1:

enter an integer:
3
enter an integer:
4
enter an integer:
2
enter an integer:
r
Could not get integer

9

Sample output 2: sscanf successfully extracted 5 from 5gf 样本输出2:sscanf从5gf成功提取了5

enter an integer:
3
enter an integer:
4
enter an integer:
5gf
enter an integer:
t
Could not get integer

12

Sample output 3: sscanf could not extract 5 from r5f and rightly so 样本输出3:sscanf无法从r5f中提取5,因此

enter an integer:
5
enter an integer:
3
enter an integer:
r5f
Could not get integer

8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM