wscanf(L"%[^\n]") 正在输入垃圾
wscanf(L"%[^\n]") is inputting garbage
我正在使用宽字符在 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”时,尽管它不接受空格,但我想要程序接受他们。有什么办法让它接受空格 和 也不输入垃圾?
很多地方都错了。
- 您不能在同一个文件中混合使用
printf
和 wprintf
,包括 stdout
(除非您一直调用 freopen
来更改流的方向...)
- 缺少
l
%l[^\n]
- space 是非字母数字,所有带有其他说明符的 "worked" 都是由于字符串不包含 space...
固定代码:
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main(void) {
setlocale(LC_ALL, "");
wchar_t sentence[30];
wprintf(L"Gimme a sentence:\n");
wscanf(L"%l29[^\n]", sentence); //Reading the line
wprintf(L"Your sentence: %ls\n", sentence); //Printing the whole line
wprintf(L"Detecting non-alphabetic wide characters"); //Detecting non-alphabetic characters
for (int i = 0; sentence[i]; i++) {
if (iswalpha(sentence[i]) == 0) {
wprintf(L"\n\"%lc\" %i\n", sentence[i], i);
wprintf(L"An illegal character has been detected here");
return 1;
}
}
return 0;
}
我正在使用宽字符在 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”时,尽管它不接受空格,但我想要程序接受他们。有什么办法让它接受空格 和 也不输入垃圾?
很多地方都错了。
- 您不能在同一个文件中混合使用
printf
和wprintf
,包括stdout
(除非您一直调用freopen
来更改流的方向...) - 缺少
l
%l[^\n]
- space 是非字母数字,所有带有其他说明符的 "worked" 都是由于字符串不包含 space...
固定代码:
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main(void) {
setlocale(LC_ALL, "");
wchar_t sentence[30];
wprintf(L"Gimme a sentence:\n");
wscanf(L"%l29[^\n]", sentence); //Reading the line
wprintf(L"Your sentence: %ls\n", sentence); //Printing the whole line
wprintf(L"Detecting non-alphabetic wide characters"); //Detecting non-alphabetic characters
for (int i = 0; sentence[i]; i++) {
if (iswalpha(sentence[i]) == 0) {
wprintf(L"\n\"%lc\" %i\n", sentence[i], i);
wprintf(L"An illegal character has been detected here");
return 1;
}
}
return 0;
}