我正在使用宽字符在c上执行hangman程序。它必须允许在单词上播放空格(程序会将其检测为非法字符)。
代码的重要部分:
int main(int argc, char** argv) {
setlocale(LC_ALL, "");
wchar_t sentence[30];
printf("Gimme a sentence:\n");
wscanf(L"%[^\n]", sentence); //Reading the line
wprintf(L"Your sentence: %ls\n", sentence); //Printing the whole line
printf("Detecting non-alphabetic wide characters"); //Detecting non-alphabetic characters
for (int i = 0; i < wcslen(sentence); i++) {
if (iswalpha(sentence[i]) == 0) {
wprintf(L"\n\"%lc\" %i\n", sentence[i], i);
printf("An illegal character has been detected here");
return (1);
}
}
return (0);
}
测试:
Gimme a sentence:
hello world
Your sentence: hello world
Detecting non-alphabetic wide characters
"o " 2
An illegal character has been detected here
我也怀疑iswalpha()也弄乱了,但是当我将“%[^ \ n]”更改为“%ls”时,尽管它不接受空格,但我希望程序接受他们。有什么办法可以接受空格和而不输入垃圾?
答案 0 :(得分:2)
很多事情错了。
printf
)上混合wprintf
和stdout
(除非始终调用freopen
来更改流的方向。) 。)l
的{{1}} 固定代码:
%l[^\n]