对不同长度的输入使用 Scanf 函数
Using the Scanf function for inputs for different lengths
我想使用 scanf 函数从用户那里获取两个数字或一个数字的输入,并将它们放入一个数组中。但是,我不确定如何使用相同的函数来获取数组的一个元素以及数组的两个元素的输入。
即如果用户输入 9 0,它应该能够成功地将其存储在数组中并移动到新代码,或者如果用户输入类似 1 的内容,它也应该能够成功地将其存储在数组中并移动到新代码新代码。
我试过将 scanf 放入 while 循环中:
int scanned_array[2] = {};
int element = 0;
while(scanf("%d", &scanned_array[element]) {
//... more code here which will have different functions depending on the input...
element++;
}
如何在仅使用 scanf 函数、while 循环、数组和 if 语句的情况下成功完成此操作?
**编辑: 只是想知道,如果我根据@nmgari 下面的建议更改我的代码,我怎么能去 if(num == 1) { ...无需按 ctrl+d?
感谢阅读!
如果我没理解错的话,您想调用一次 scanf() 并读取一个或两个数字。
像这样的东西应该可以工作:
int scanned_array[2];
int num_input = 0;
num_input = scanf("%d %d", &scanned_array[0], &scanned_array[1]);
if(num_input == 1)
{
//Do something
}
else if (num_input == 2)
{
//Do somethine else
}
无论如何,你需要考虑到数组中的第二个元素可能永远得不到值。
你应该用 fgets()
读取输入行并用 sscanf()
解析它:
char buf[120];
if (fgets(buf, sizeof buf, stdin)) {
int i1, i2;
switch (sscanf(buf, "%d%d", &i1, &i2)) {
case 2:
/* the user entered 2 integers */
handle_2_integers(i1, i2);
break;
case 1:
/* the user entered a single integer */
handle_1_integer(i1);
break;
default:
/* the user did not enter a number */
handle_invalid_input();
break;
}
} else {
/* the input stream is at end of file or encountered an error */
handle_end_of_file();
}
如果你 运行 scanf("%d%d", &i1, &i2)
, scanf()
将继续读取输入流直到文件末尾,或者读取了 2 个整数或输入了一个不能被输入的字符转换为整数,可能会读取多行输入以发生其中一种情况。如果你想处理单行的输入,你不能直接使用scanf()
。
可以使用单独的空白扫描。
扫描集 %1[ \t\r\n]
将逐个字符地捕获空白字符。循环将在非空白或换行符处退出。
然后尝试扫描一个整数。
#include <stdio.h>
#include <stdlib.h>
int main ( void) {
char space[2] = "";
int scanned = 0;
int scanned_array[2] = { 0};
while ( 1) {
while ( 1 == scanf ( "%1[ \t\r\n]", space)) {
if ( '\n' == space[0]) {
break;
}
}
if ( '\n' == space[0]) {
break;
}
if ( 1 != scanf ( "%d", &scanned_array[scanned])) {
fprintf ( stderr, "bad input\n");
return 1;
}
++scanned;
if ( 2 <= scanned) {
break;
}
}
for ( int each = 0; each < scanned; ++each) {
printf ( "%d\n", scanned_array[each]);
}
return 0;
}
我想使用 scanf 函数从用户那里获取两个数字或一个数字的输入,并将它们放入一个数组中。但是,我不确定如何使用相同的函数来获取数组的一个元素以及数组的两个元素的输入。
即如果用户输入 9 0,它应该能够成功地将其存储在数组中并移动到新代码,或者如果用户输入类似 1 的内容,它也应该能够成功地将其存储在数组中并移动到新代码新代码。
我试过将 scanf 放入 while 循环中:
int scanned_array[2] = {};
int element = 0;
while(scanf("%d", &scanned_array[element]) {
//... more code here which will have different functions depending on the input...
element++;
}
如何在仅使用 scanf 函数、while 循环、数组和 if 语句的情况下成功完成此操作?
**编辑: 只是想知道,如果我根据@nmgari 下面的建议更改我的代码,我怎么能去 if(num == 1) { ...无需按 ctrl+d?
感谢阅读!
如果我没理解错的话,您想调用一次 scanf() 并读取一个或两个数字。
像这样的东西应该可以工作:
int scanned_array[2];
int num_input = 0;
num_input = scanf("%d %d", &scanned_array[0], &scanned_array[1]);
if(num_input == 1)
{
//Do something
}
else if (num_input == 2)
{
//Do somethine else
}
无论如何,你需要考虑到数组中的第二个元素可能永远得不到值。
你应该用 fgets()
读取输入行并用 sscanf()
解析它:
char buf[120];
if (fgets(buf, sizeof buf, stdin)) {
int i1, i2;
switch (sscanf(buf, "%d%d", &i1, &i2)) {
case 2:
/* the user entered 2 integers */
handle_2_integers(i1, i2);
break;
case 1:
/* the user entered a single integer */
handle_1_integer(i1);
break;
default:
/* the user did not enter a number */
handle_invalid_input();
break;
}
} else {
/* the input stream is at end of file or encountered an error */
handle_end_of_file();
}
如果你 运行 scanf("%d%d", &i1, &i2)
, scanf()
将继续读取输入流直到文件末尾,或者读取了 2 个整数或输入了一个不能被输入的字符转换为整数,可能会读取多行输入以发生其中一种情况。如果你想处理单行的输入,你不能直接使用scanf()
。
可以使用单独的空白扫描。
扫描集 %1[ \t\r\n]
将逐个字符地捕获空白字符。循环将在非空白或换行符处退出。
然后尝试扫描一个整数。
#include <stdio.h>
#include <stdlib.h>
int main ( void) {
char space[2] = "";
int scanned = 0;
int scanned_array[2] = { 0};
while ( 1) {
while ( 1 == scanf ( "%1[ \t\r\n]", space)) {
if ( '\n' == space[0]) {
break;
}
}
if ( '\n' == space[0]) {
break;
}
if ( 1 != scanf ( "%d", &scanned_array[scanned])) {
fprintf ( stderr, "bad input\n");
return 1;
}
++scanned;
if ( 2 <= scanned) {
break;
}
}
for ( int each = 0; each < scanned; ++each) {
printf ( "%d\n", scanned_array[each]);
}
return 0;
}