如何处理字符和整数的混合输入
How to handle mixed input of chars and ints
如何处理以下情况。
要求用户输入他的命令:
- 用户只输入了一个数字,例如1
- 用户输入一个字符串,例如 hello
- 用户输入一个字符串,例如 hello 1 2
终端可能看起来像
Enter: 1 -- programm do somehting(not important)
Enter: hello -- programm do somehting(not important)
Enter: hello 1 2 -- programm do somehting(not important)
我的代码
printf("Enter:");
scanf("%[^\n]s", command_player);
getchar()
for(size_t i = 0; i < strlen(command); i++)
{
if(isdigit(command[i]))
{
has_digit = 1;
}
if(isalpha(command[i]))
{
has_letter = 1;
}
}
and then
if(has_digit == 1 && has_letter == 0)
//do something
if(has_digit == 0 && has_letter == 1)
//do something
if(has_digit == 1 && has_letter == 1)
//do something
但是我遇到了一个问题,如果我在 ifs 中输入另一种数据类型,我的程序就会崩溃
您可以通过使用 fgets()
读取输入行并尝试使用 sscanf()
以不同方式解析它来实现您的目标:
#include <stdio.h>
int main() {
char buf[256];
char word[256];
int n1, n2;
char endc;
// loop for ever: equivalent to while (1) {}
for (;;) {
// output the prompt
printf("Enter: ");
// flush the output to make sure if is visible:
// output is normally line buffered, but the prompt does
// not end with a newline so it is still in the stdout buffer
// most systems will flush the output when a read operation
// is requested, but some don't so `fflush(stdout)` ensures
// the prompt is visible on all systems.
flush(stdout);
// read a line of input, and break from the loop at end of file
if (!fgets(buf, sizeof buf, stdin))
break;
// try matching different input patterns:
// `sscanf()` returns the number of successful conversions,
if (sscanf(buf, "%d %c", &n1, &endc) == 1) {
// `sscanf()` returned `1` if there is exactly a number
// preceded and/or followed by optional whitespace
// but no further character.
printf("user entered a single number %d\n", n1);
} else
if (sscanf(buf, " %255[a-zA-Z] %c", word, &endc) == 1) {
// this second `sscanf()` returns 1 if there is exactly
// a single word (a sequence of uppercase or lowercase
// letters) with optional initial and training whitespace.
printf("user entered a single word %s\n", word);
} else
if (sscanf(buf, " %255[a-zA-Z] %d %d %c", word, &n1, &n2, &endc) == 3) {
// this third `sscanf()` returns 3 if there is exactly
// a word followed by 2 numbers with optional initial
// and training whitespace.
printf("user entered a word %s and 2 numbers %d and %d\n",
word, n1, n2);
} else {
printf("user input does not match a known pattern: %s", buf);
}
}
return 0;
}
可以扩展格式字符串以包含单词的其他字符并进行修改以匹配其他输入模式。
如何处理以下情况。 要求用户输入他的命令:
- 用户只输入了一个数字,例如1
- 用户输入一个字符串,例如 hello
- 用户输入一个字符串,例如 hello 1 2
终端可能看起来像
Enter: 1 -- programm do somehting(not important)
Enter: hello -- programm do somehting(not important)
Enter: hello 1 2 -- programm do somehting(not important)
我的代码
printf("Enter:");
scanf("%[^\n]s", command_player);
getchar()
for(size_t i = 0; i < strlen(command); i++)
{
if(isdigit(command[i]))
{
has_digit = 1;
}
if(isalpha(command[i]))
{
has_letter = 1;
}
}
and then
if(has_digit == 1 && has_letter == 0)
//do something
if(has_digit == 0 && has_letter == 1)
//do something
if(has_digit == 1 && has_letter == 1)
//do something
但是我遇到了一个问题,如果我在 ifs 中输入另一种数据类型,我的程序就会崩溃
您可以通过使用 fgets()
读取输入行并尝试使用 sscanf()
以不同方式解析它来实现您的目标:
#include <stdio.h>
int main() {
char buf[256];
char word[256];
int n1, n2;
char endc;
// loop for ever: equivalent to while (1) {}
for (;;) {
// output the prompt
printf("Enter: ");
// flush the output to make sure if is visible:
// output is normally line buffered, but the prompt does
// not end with a newline so it is still in the stdout buffer
// most systems will flush the output when a read operation
// is requested, but some don't so `fflush(stdout)` ensures
// the prompt is visible on all systems.
flush(stdout);
// read a line of input, and break from the loop at end of file
if (!fgets(buf, sizeof buf, stdin))
break;
// try matching different input patterns:
// `sscanf()` returns the number of successful conversions,
if (sscanf(buf, "%d %c", &n1, &endc) == 1) {
// `sscanf()` returned `1` if there is exactly a number
// preceded and/or followed by optional whitespace
// but no further character.
printf("user entered a single number %d\n", n1);
} else
if (sscanf(buf, " %255[a-zA-Z] %c", word, &endc) == 1) {
// this second `sscanf()` returns 1 if there is exactly
// a single word (a sequence of uppercase or lowercase
// letters) with optional initial and training whitespace.
printf("user entered a single word %s\n", word);
} else
if (sscanf(buf, " %255[a-zA-Z] %d %d %c", word, &n1, &n2, &endc) == 3) {
// this third `sscanf()` returns 3 if there is exactly
// a word followed by 2 numbers with optional initial
// and training whitespace.
printf("user entered a word %s and 2 numbers %d and %d\n",
word, n1, n2);
} else {
printf("user input does not match a known pattern: %s", buf);
}
}
return 0;
}
可以扩展格式字符串以包含单词的其他字符并进行修改以匹配其他输入模式。