C - 基于用户决策的 do/while 循环无法正常工作
C - user decision based do/while loop not working correctly
我有一个问题,经过多次测试我认为这是因为我不了解输入缓冲区的工作原理。
我有一个 while 循环,它应该继续迭代,直到用户输入 "no" 停止迭代。
我有两个问题。
- 无论用户输入 "no" 或任何不等于 "yes"
的内容,while 都不会停止迭代
- 如您所见,第二个周期的输出有问题。该程序不会要求用户输入字符串并跳过该步骤,就像用户只需键入 ENTER 一样。
代码:
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] = '[=11=]'; //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);
输出
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?
您正在创建一个 3 个字节的字符数组,然后将三个以上的字节存储到其中。不要忘记最后会有一个空值。由于您没有分配足够的 space,您正在覆盖其他内存位置,这将始终产生未定义的行为。
另请注意,这里的 scanf 非常不安全。像这样初始化字符数组也是无效的:char foo[3]="";
如果用户输入 "yes"
,您的程序不会终止,因为内部 for
循环:
#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] = '[=10=]'; //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);
}
另见 I am not able to flush stdin and http://c-faq.com/stdio/stdinflush2.html
我有一个问题,经过多次测试我认为这是因为我不了解输入缓冲区的工作原理。
我有一个 while 循环,它应该继续迭代,直到用户输入 "no" 停止迭代。
我有两个问题。
- 无论用户输入 "no" 或任何不等于 "yes" 的内容,while 都不会停止迭代
- 如您所见,第二个周期的输出有问题。该程序不会要求用户输入字符串并跳过该步骤,就像用户只需键入 ENTER 一样。
代码:
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] = '[=11=]'; //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);
输出
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?
您正在创建一个 3 个字节的字符数组,然后将三个以上的字节存储到其中。不要忘记最后会有一个空值。由于您没有分配足够的 space,您正在覆盖其他内存位置,这将始终产生未定义的行为。
另请注意,这里的 scanf 非常不安全。像这样初始化字符数组也是无效的:char foo[3]="";
如果用户输入 "yes"
,您的程序不会终止,因为内部 for
循环:
#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] = '[=10=]'; //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);
}
另见 I am not able to flush stdin and http://c-faq.com/stdio/stdinflush2.html