为什么 scanf 跳过获取字符串输入?
Why does scanf skip getting string input?
我正在尝试将多个单词输入和多行输入到一个数组中。但是在某处代码跳过了获取输入并跳到结束程序。我试过在“%s”(或“%s”)前后添加 space 但它不起作用(可能是因为它在循环中?)。非常感谢任何人的帮助!如果我输入超过两三个词,它也会开始表现得很奇怪:(
我的目标是找出特定字母在所有这些单词和行中出现了多少次。
#include <stdio.h> //include standard library
int main(){
int lineCount, occuranceCount;
printf("How many lines are you going to enter?");
scanf("%d", &lineCount);
char input[lineCount][100], searchChar;
for(int i=0; i<lineCount; i++){
printf("Please enter line #%d (100 characters of less):",i+1);
scanf("%s", &input[i]);
}
printf("What letter do you want to check the frequence in those lines? ");
scanf("%c", &searchChar);
for(int j=0; j<lineCount; j++){
for(int k=0; k<100; k++){
if(input[j][k] != '[=10=]'){
if(input[j][k]==searchChar)
occuranceCount++;
}
}
}
printf("The letter occurs for %d time", occuranceCount);
return 0;
}
scanf(" %c", &searchChar);
^
你需要这里的 space 来消耗 stdin
中的任何 \n
。
此外,scanf()
将按照您的想法阅读一个单词而不是一行(space 个分隔的单词)。
而且最好使用 strlen(input[j])
来了解您应该阅读多少内容。
另一件事,在循环中使用 size_t
而不是 int
。
将occuranceCount
初始化为0
。
同时为了避免 buffer overrun
漏洞利用,请在您的代码中使用 scanf("%99s", input[i]);
。
要阅读一行,您可以使用 fgets()
.
1) 改变
scanf("%c", &searchChar); --> scanf(" %c", &searchChar);
删除之前 scanf
留在输入缓冲区中的任何 \n
2) 改变
for(int k=0; k<100; k++){ --> for(int k=0; k<strlen(input[j]); k++){
以避免读取超出实际用户输入的范围。
除此之外:
切勿 scanf("%s", &input[i]);
因为用户可能会溢出您的输入缓冲区。至少将其更改为:scanf("%99s", &input[i]);
但考虑使用 fgets
而不是
使用 fgets
你的程序可能是:
#include <stdio.h>
int main(){
size_t lineCount = 0, occuranceCount = 0;
printf("How many lines are you going to enter?");
scanf("%zu ", &lineCount);
if (lineCount == 0) return 0; // Input error
char input[lineCount][100], searchChar;
for(size_t i=0; i<lineCount; i++){
printf("Please enter line #%zu (100 characters of less):",i+1);
fgets(input[i], 100, stdin);
}
printf("What letter do you want to check the frequence in those lines? ");
scanf("%c", &searchChar);
for(size_t j=0; j<lineCount; j++){
for(size_t k=0; k<strlen(input[j]); k++){
if(input[j][k]==searchChar) occuranceCount++;
}
}
printf("The letter occurs for %zu time", occuranceCount);
return 0;
}
使用 %s 的 scanf 始终读取直到遇到 ' '(空格)或 '\n'(换行符),因此您始终只能使用 scanf("%s", s1) 读取一个单词。 ..
如果你想读取空格或换行符你必须使用gets,或者fgets(比gets更安全)
我正在尝试将多个单词输入和多行输入到一个数组中。但是在某处代码跳过了获取输入并跳到结束程序。我试过在“%s”(或“%s”)前后添加 space 但它不起作用(可能是因为它在循环中?)。非常感谢任何人的帮助!如果我输入超过两三个词,它也会开始表现得很奇怪:( 我的目标是找出特定字母在所有这些单词和行中出现了多少次。
#include <stdio.h> //include standard library
int main(){
int lineCount, occuranceCount;
printf("How many lines are you going to enter?");
scanf("%d", &lineCount);
char input[lineCount][100], searchChar;
for(int i=0; i<lineCount; i++){
printf("Please enter line #%d (100 characters of less):",i+1);
scanf("%s", &input[i]);
}
printf("What letter do you want to check the frequence in those lines? ");
scanf("%c", &searchChar);
for(int j=0; j<lineCount; j++){
for(int k=0; k<100; k++){
if(input[j][k] != '[=10=]'){
if(input[j][k]==searchChar)
occuranceCount++;
}
}
}
printf("The letter occurs for %d time", occuranceCount);
return 0;
}
scanf(" %c", &searchChar);
^
你需要这里的 space 来消耗 stdin
中的任何 \n
。
此外,scanf()
将按照您的想法阅读一个单词而不是一行(space 个分隔的单词)。
而且最好使用 strlen(input[j])
来了解您应该阅读多少内容。
另一件事,在循环中使用 size_t
而不是 int
。
将occuranceCount
初始化为0
。
同时为了避免 buffer overrun
漏洞利用,请在您的代码中使用 scanf("%99s", input[i]);
。
要阅读一行,您可以使用 fgets()
.
1) 改变
scanf("%c", &searchChar); --> scanf(" %c", &searchChar);
删除之前 scanf
\n
2) 改变
for(int k=0; k<100; k++){ --> for(int k=0; k<strlen(input[j]); k++){
以避免读取超出实际用户输入的范围。
除此之外:
切勿 scanf("%s", &input[i]);
因为用户可能会溢出您的输入缓冲区。至少将其更改为:scanf("%99s", &input[i]);
但考虑使用 fgets
而不是
使用 fgets
你的程序可能是:
#include <stdio.h>
int main(){
size_t lineCount = 0, occuranceCount = 0;
printf("How many lines are you going to enter?");
scanf("%zu ", &lineCount);
if (lineCount == 0) return 0; // Input error
char input[lineCount][100], searchChar;
for(size_t i=0; i<lineCount; i++){
printf("Please enter line #%zu (100 characters of less):",i+1);
fgets(input[i], 100, stdin);
}
printf("What letter do you want to check the frequence in those lines? ");
scanf("%c", &searchChar);
for(size_t j=0; j<lineCount; j++){
for(size_t k=0; k<strlen(input[j]); k++){
if(input[j][k]==searchChar) occuranceCount++;
}
}
printf("The letter occurs for %zu time", occuranceCount);
return 0;
}
使用 %s 的 scanf 始终读取直到遇到 ' '(空格)或 '\n'(换行符),因此您始终只能使用 scanf("%s", s1) 读取一个单词。 ..
如果你想读取空格或换行符你必须使用gets,或者fgets(比gets更安全)