简体   繁体   English

C-基于用户决策的do / while循环无法正常工作

[英]C - user decision based do/while loop not working correctly

I have a problem which after many tests I think it's due to me not understanding how the input buffer works. 我有一个问题,经过很多测试,我认为这是由于我不了解输入缓冲区的工作原理。

I have a while cycle which should continue to iterate until the user types "no" to stop the iteration. 我有一个while周期,应该继续进行迭代,直到用户键入“ no”以停止迭代为止。

I have two problems. 我有两个问题。

  1. The while never stops to iterate regardless the user enters "no" or whatever is not equal to "yes" 无论用户输入“否”或不等于“是”的那一刻,while都不会停止迭代
  2. As you can see the output at the second cycle has a problem. 如您所见,第二个周期的输出有问题。 The program doesn't ask the user to input a string and skip that step, like the user just types ENTER. 该程序不要求用户输入字符串并跳过该步骤,就像用户只是键入ENTER一样。

CODE: 码:

int foo = 0;

do{

  int i, cycles;
  char array[MAX_LENGTH+1];



  for(cycles=0; cycles < MAX_READ_CYCLES; cycles++){

    i=0;

    printf("\n\nEnter a string: ");

    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF) {
      array[i] = ch;
      i++;
    }

    array[i] = '\0'; //string terminator


    printf("String you entered: %s\n", array);

    printf("\nDo you want to continue? 1: yes / 0: no \n");
    scanf("%d", &foo);

  }

} while( foo == 1);

OUTPUT 输出值

Enter a string: test
String you entered: test

Do you want to continue? 1: yes / 0: no
0

Enter a string: String you entered: 

Do you want to continue? 1: yes / 0: no
3

Enter a string: String you entered: 

Do you want to continue?

Your program doesn't terminate if the user enters "yes" because of the inner for loop: 如果用户由于内部for循环而输入"yes" ,则您的程序不会终止:

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
#define MAX_READ_CYCLES 100

int main() {
    int cycles = 0;
    char foo[4];
    do {
        char array[MAX_LENGTH + 1];

        printf("\n\nEnter a string: ");

        char ch;
        int i = 0;
        while ((ch = getchar()) != '\n' && ch != EOF) {
            array[i] = ch;
            i++;
        }

        array[i] = '\0'; //string terminator


        printf("String you entered: %s\n", array);

        printf("\nDo you want to continue?");
        scanf("%s", foo);

        cycles++;

        while ((ch = getchar()) != '\n' && ch != EOF); // force drop stdin

    } while (strcmp(foo, "yes") == 0 && cycles < MAX_READ_CYCLES);
}

Also see I am not able to flush stdin and http://c-faq.com/stdio/stdinflush2.html 另请参阅我无法刷新标准输入http://c-faq.com/stdio/stdinflush2.html

You are creating a character array of 3 bytes and then storing more than three bytes into it. 您正在创建一个3字节的字符数组,然后在其中存储三个以上的字节。 Don't forget that there will be a null tacked onto the end of that. 别忘了在结尾处添加一个空值。 Since you are not allocating enough space you are overwriting other memory locations which will always create undefined behavior. 由于您没有分配足够的空间,因此您将覆盖其他内存位置,这将始终创建未定义的行为。

Note, also, that scanf here is very unsafe. 还要注意,这里的scanf是非常不安全的。 It's also not valid to initialize a character array like this: char foo[3]=""; 初始化这样的字符数组也是无效的: char foo[3]="";

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

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