简体   繁体   English

C:如何使用scanf存储未知数量的输入,以便将输入的每个单词存储在不同的变量中?

[英]C: How can I store an unknown number of inputs using scanf so that each word of the input is stored in a different variable?

So far i have: 到目前为止,我有:

    char r[4][10];

    printf("Enter an option: \n");
    scanf("%s %s %s %s %s", r[0],r[1],r[2], r[3], r[4]);
    printf("%s %s %s %s %s\n", r[0], r[1], r[2], r[3], r[4]);

This works as long as 5 inputs are given. 只要给出5个输入,就可以使用。 Right now if you type less then 5 inputs and hit enter the program continues to ask for input until it has all 5 values but I want the scanf to stop and the program to continue if less then 5 inputs are entered. 现在,如果您键入的输入少于5个,然后按Enter键,程序将继续要求输入,直到它具有所有5个值,但是如果输入的输入少于5个,我希望scanf停止并且程序继续。

You'll do best to read a line with fgets() and parse it with sscanf() . 最好使用fgets()读取一行,并使用sscanf()对其进行解析。 If there will be no more than 5 words on a line: 如果一行上的单词数不超过5个:

char r[5][10];
char line[4096];

printf("Enter an option: \n");
if (fgets(line, sizeof(line), stdin) != 0)
{
    int num = sscanf(line, "%9s %9s %9s %9s %9s", r[0],r[1],r[2], r[3], r[4]);
    for (int i = num; i < 5; i++)
        r[i][0] = '\0';
    printf("%s %s %s %s %s\n", r[0], r[1], r[2], r[3], r[4]);
}

Note that you only had r[4][10] but were using subscript 4 — bad move. 请注意,您只有r[4][10]但使用的是下标4-错误的动作。

A better way to handle it would be a loop that scans over the line, reading a word at a time until there is no more space in the array or no more words in the line. 一种更好的处理方法是循环扫描整个行,一次读取一个单词,直到数组中没有空间或行中没有单词为止。 It's a little trickier, though. 不过,这有点棘手。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我的 scanf 变量是一个浮点数并且用户输入了一个字符,我如何提示他们输入一个数字? 假设 scanf 在 do while 循环中 - if my scanf variable is a float and a user inputs a character how can i prompt them to input a number? assuming the scanf is inside a do while loop 如何使用 '%c' 和 scanf() 输入字符串 - How can I input a string using '%c' and scanf() 将十六进制输入存储到 int 变量中而不使用 scanf() function in C - Store hex input into int variable without using scanf() function in C 使用scanf和C中的循环对字符或数字进行可变长度输入 - Variable length input of a character or a number using scanf and a loop in C C:我怎样才能使 scanf() 输入具有两种格式之一? - C: How can I make it so scanf() input has one of two formats? 如何从.txt文件中读取已知数量的未知大小的字符串,并将每一行存储在矩阵的一行中(用C表示)? - How can I read a known number of strings of unknown size from a .txt file and store each line in a line of a matrix (in C)? 在 C 中,如何将存储在变量中的多个单词字符串分解为一个数组,每个单词都保存一个数组值 - In C how do I break a multiple word string stored in a variable into an array with each word holding an array value C 语言刽子手游戏,我如何将 scanf 与掩码字联系起来,并为每个不正确的输入提供一个计数器 inc? - C language Hangman game, how do i link the scanf with the masked word and have a counter inc for each incorrect input? 每次循环迭代时,如何将不同的值存储在变量中,然后将它们全部添加到 c 中。? - How can I store different values in a variable each time a loop iterates and then add them all together in c .? 如何使用 scanf() 从同一行输入两种不同的数据类型? - How can I take input of two different data type using scanf() from same line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM