使用的getchar()取多个值。 [英] Using getchar() to take multiple values.

查看:186
本文介绍了使用的getchar()取多个值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能以某种方式使用的getchar(),以通过命令行运行,直到我打了'\ N'。 说我想在我的所有输入,如值进行扫描。

Could I somehow use getchar() to run through a command line until I hit a '\n'. Say I want to scan in all the values I enter such as.

21 23 1 78 54'\ N'。我想扫描所有的五个值到一个数组。我无法扫描他们进来是因为每一个值之间的距离呢?或者是有,我们可以用它来做到这一点的函数?

21 23 1 78 54 '\n'. I want to scan all the five values into an array. I am unable to scan them in. Is it because of the spacing between each value? Or is there a function we could use to do it ?

在此先感谢

推荐答案

如果您不使用的getchar()然后我用一个简单的解决方案<$ C弯曲$ C> scanf函数与%D 转换说明:

If you are not bent on using getchar() then I have a straightforward solution using scanf with the %d conversion specifier:

while (i < ARRAY_SIZE && scanf("%d", &a[i]) == 1)
    i++ ;

%D 转换符告诉 scanf函数来跳过任何前导空格和读取到下一个非数字字符。返回值是成功转换和分配数量。因为我们要读取一个整数值,则返回值应该是1成功。

The %d conversion specifier tells scanf to skip over any leading whitespace and read up to the next non-digit character. The return value is the number of successful conversions and assignments. Since we're reading a single integer value, the return value should be 1 on success.

这篇关于使用的getchar()取多个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆