getchar() 实际上并没有从用户那里获取输入
getchar() doesn't actually take input from user
我正在用C++编写一个灵活的命令行(但不会太长!)菱形方形生成器。我刚刚写完了用户输入的一半。然而,在最后一个命令中,输入"slips",并自动向getchar()输入一行换行符。我已经采取了预防措施,以确保它不会出现任何类型的溢出,即同时刷新stdin和stdout。问题仍然存在。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main () {
unsigned long seed = 0, x = 0, y = 0, initial = 0, range = 0;
int smooth = 0, fail = 1;
char flagchar1 = 'n';
printf("Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. ");
do {
printf("Please input the seed (this is a positive integer):n");
fail = scanf("%lu", &seed);
while (fail == 0) {
printf("Try again, smartass.n");
fail = scanf("%lu", &seed);
}
fail = 1;
printf("Now input the x, or horizontal, size of your grid:n");
fail = scanf("%lu", &x);
while (fail == 0) {
printf("An integer. Not a string. An integer. You can do that, can't you?n");
fail = scanf("%lu", &x);
}
fail = 1;
printf("Now input the y, or vertical, size of your grid:n");
fail = scanf("%lu", &y);
while (fail == 0) {
printf("What was that supposed to be? An integer, please.n");
fail = scanf("%lu", &y);
}
fail = 1;
printf("Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):n");
fail = scanf("%lu", &initial);
while (initial == 0 || initial > 256 || fail == 0) {
printf("ahahahahaha how HIGH do you have to be just to HAVE that hieght........n");
fail = scanf("%lu", &initial);
}
fail = 1;
printf("Now input the range of the heights on your grid (this must be equal to or less than 256):n");
scanf("%lu", &range);
while (range >= 256 || fail == 0) {
printf("What did I say about being equal to or less than 256? Give me something reasonable to work with here.n");
fail = scanf("%lu", &range);
}
fail = 1;
printf("Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!n");
fail = scanf("%d", &smooth);
while (fail == 0) {
printf("That... was not a number.n");
fail = scanf("%d", &smooth);
}
fail = 1;
printf("nOkay. Are these the values you want?n Seed: %lun Width: %lun Length: %lun Height: %lun Range: %lun Smoothness: %dnDo you want to keep these? Type Y/n.n", seed, x, y, initial, range, smooth);
fflush(stdin);
fflush(stdout);
flagchar1 = getchar();
} while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != 'n');
}
这是我的输出,程序已经结束(如果我从while()
中删除&& flagchar1 != 'n'
,程序只重复整个do-while循环):
欢迎使用我的菱形发电机!这还不是全部功能,所以我只是让你一个接一个地输入变量。请输入种子(这是一个正整数):12345678现在输入网格的x或水平大小:40现在输入网格的y或垂直大小:30现在输入您希望网格的高度(从1到256):1288哈哈哈哈,你必须有多高才能拥有它。。。。。。。。128现在输入网格上的高度范围(必须等于或小于256):30还有一个变量!现在,我需要您输入网格的平滑度。数字越小,网格越尖。你可以说这是负面的,但要小心!10可以这些是你想要的价值观吗?种子:12345678宽度:40长度:30高度:128范围:30平滑度:10你想保留这些吗?类型Y/n。
发生了什么,我该如何解决?
附言:我知道我的输入验证基本上是无用的。我们也非常感谢您的帮助。
让循环的结尾看起来像这样:
// Ignore remaining characters on current line.
int ch;
while( (ch = getchar()) != EOF && ch != 'n')
;
// fetch first character on next line
flagchar1 = getchar();
} while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != 'n');
上次调用scanf
后,您将把'n'
留在stdin中。
您不能依赖fflush(stdin)
具有任何特定行为。对输入流调用fflush
的结果是未定义的。请参阅使用fflush(stdin)
代码的行为完全符合您的要求。如果用户输入"y"、"y"或enter,while循环中的其中一个条件将为false,从而导致它退出。
你想要的是:
while (flagchar1 == 'y' || flagchar1 == 'Y' || flagchar1 == 'n');
编辑:我还将删除fflush(stdin),并用fgets()替换getchar()。这将保证在不必使用fflush的情况下读取整行,这可能是问题所在。
我猜你在Linux上?这在Windows中的VS上运行良好。它提示,从键盘上读取,如果检查,则包含问题中正确的"y"或"y"。
我可能建议您尝试将最后一次扫描更改为:
fail = scanf("%d ", &smooth);
你也可以试着调用fpurge()而不是fflush(),但这不是标准的,我认为格式字符串末尾的空格会得到你想要的。
尾部空格将要求scanf在输入数据中使用任何额外的空白(包括换行符)。fflush()可能不会执行您想要的输入操作。
我怀疑无论你使用什么系统,都会将回车留在流中,如果你将flagchar1打印为int,你会得到10?
建议:
- 使用C++流
- 在比较字符之前,请使用
tolower
或toupper
- 使用
std::string
使用fgets, gets, fflush, strcmp,
的C语言在这方面存在许多问题。C++语言已经解决了std::stream
类中的许多问题。
由于您没有使用C++功能,因此应该将C++标记更改为C。
尝试
}while(flagchar1!='y'|flagcchar1!='y'|flacchar1!='\n');
而不是
}while(flagchar1!='y'&flagcchar1!='y'&flagchar1!='\n');
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 如何从用户那里获取输入并将其存储在向量中?
- 从用户那里获取数组大小的值
- 代码在执行之前终止,并且没有从用户那里获取输入
- 我无法在某个点(从用户那里)获得输入,即使没有错误弹出编译
- 从用户那里获得无限量的输入,而不是打印最大的3个,而不使用数组
- 有没有更简单的方法可以从用户那里获取三个数字并按升序打印它们?
- 只要用户输入,如何从用户那里获取整数?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?
- 如何从用户那里接收多个字符串并将这些字符串添加到 c++ 中的数组中?
- 如何从用户那里获取单个字符,直到他输入"."或输入
- C++ 从用户那里获取未知数据类型输入
- 如何从用户那里获取方程的输入并在 c++ 中对其进行评估
- 从用户那里获取"yyyy/mm/dd, hh:mm"形式的日期输入并验证C++中的日期?
- 我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
- 我在使用 cin.getline() 从用户那里获取 3 个字符串并使用指针数组来存储它们时遇到问题
- 如何制作 n*n 矩阵的输出文件,通过从用户那里获取 n,并成对生成的数字
- 如何从用户那里获取具有预定义字符串长度的字符串输入C++
- 我正在编写一个代码,该代码从用户那里为公司中的五个项目输入数量
- 从用户那里获取输入,直到他输入一个数字