将单个字符扫描到数组中 C 编程

发布于 2024-12-07 05:15:13 字数 329 浏览 0 评论 0原文

我在将字符扫描到数组中时遇到问题。每次我这样做都会跳过下一次扫描并进入下一次。我知道发生了什么,因为输入还会在输入中添加“\n”,但我不知道如何纠正其原因。这是一些示例代码:

char charray [MAX], ffs;
int inarray [MAX], i;


for (i = 0; i < MAX; i++)
{
    charray[i] = getchar();
    printf ("%c\n",charray[i]);
    scanf ("%d", &inarray[i]);
    printf ("%d\n",inarray[i]);
}

I am having a problem scanning chars into an array. Every time I do it will skip the next scan and go to the next. I know what is happening because the input also adds '\n' to the input but I do not know how to remedy the cause of it. Here is some sample code:

char charray [MAX], ffs;
int inarray [MAX], i;


for (i = 0; i < MAX; i++)
{
    charray[i] = getchar();
    printf ("%c\n",charray[i]);
    scanf ("%d", &inarray[i]);
    printf ("%d\n",inarray[i]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀中猫帐中妖 2024-12-14 05:15:13

你可以这样做。

while((c = getchar()) != '\n')
{
    putchar(c);
}

这可能会解决你的问题。或者你也可以一直到 EOF。

You can do like this.

while((c = getchar()) != '\n')
{
    putchar(c);
}

this may solve your problem. or you can go till EOF also.

怪我太投入 2024-12-14 05:15:13

您正在使用 2 个函数读取标准输入:getchar()scanf()。您需要了解它们是如何工作的。

getchar() 很简单:它返回输入流中的下一个可用字符(或等待一个字符或返回 EOF

scanf("%d", . ..) 更复杂:首先,它选择性地丢弃空格(空格、回车、制表符……),然后它读取尽可能多的字符来表示一个整数,并在第一个可以匹配的字符处停止。不能用于整数,例如'\n'

当您将它们置于循环中时,您的 getchar() 调用将获取停止 scanf() 的字符以及下一个 scanf()将从那里开始。

如果您的输入类似于 "q1w22e333r4444" (MAX == 4),您的程序将运行。

如果您的输入类似于

q 1
w 22
e 333
r 4444

第一次循环后的内容(其中 charray[0] 获取 'q'inarray[0] 获取1),getchar() 将获取 '\n',而 'w' 则“准备就绪” scanf,这当然会失败......然后被下一个“捕获” getchar();并且 "22" 在第三次循环中被分配(到 inarray[2])。

因此,您需要检查您的代码。

此外,scanf() 返回一个值。 使用该值

if (scanf("%d", &inarray[i]) != 1) /* error */;

You are reading from the standard input with 2 functions: getchar() and scanf(). You need to understand how they work.

getchar() is easy: it returns the next available character in the input stream (or waits for one or returns EOF)

scanf("%d", ...) is more complex: first, it optionally discards whitespace (spaces, enters, tabs, ...), then it reads as many characters as possible to represent an integer, and stops at the first character that can't be used for integers, like a '\n'.

As you have them in a loop, your getchar() call will get the character that stopped the scanf() and the next scanf() will procedd from there.

If your input is something like "q1w22e333r4444" (with MAX == 4), your program will work.

If your input is something like

q 1
w 22
e 333
r 4444

after the first time through the loop (where charray[0] gets 'q' and inarray[0] gets 1), the getchar() will get '\n' leaving the 'w' "ready" for scanf, which of course fails ... and is then "caught" by the next getchar(); and the "22" gets assigned in the 3rd time through the loop (to inarray[2]).

So, you need to review your code.

Also, scanf() returns a value. Use that value

if (scanf("%d", &inarray[i]) != 1) /* error */;
篱下浅笙歌 2024-12-14 05:15:13

实际上,您应该直接将字符串扫描到数组中,而不是使用 scanf("%s",&charray); 扫描字符。

但是,如果您添加 while(getchar( ) != '\n' ); 语句。这将获取 '\n' 之前的所有字符。

charray[i] = getchar();
do{
    c = getchar();
}while(c != '\n' && c!= EOF);
printf ("%c\n",charray[i]);
scanf ("%d", &inarray[i]);
do{
    c = getchar();
}while(c != '\n' && c!= EOF);    
printf ("%d\n",inarray[i]);

You should actually scan a string into the array directly, rather than characters using scanf("%s",&charray);

However your code will work if you add a while(getchar() != '\n' ); statement. This will get all characters till the '\n'.

charray[i] = getchar();
do{
    c = getchar();
}while(c != '\n' && c!= EOF);
printf ("%c\n",charray[i]);
scanf ("%d", &inarray[i]);
do{
    c = getchar();
}while(c != '\n' && c!= EOF);    
printf ("%d\n",inarray[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文