将数字字符数组转换为整数
我正在创建一个控制台程序,要求用户输入 0 到 100 之间的整数,然后输出每个数字以及输入的次数。用户输入的每个数字均以空格分隔,输入 0 时结束输入。到目前为止,我正在使用 cin.getline 将数字存储在字符数组中。这限制了操作数字的能力,因为它们存储为 ASCII 字符。如果用户输入 10,我希望能够将 10 作为单个整数存储到新数组中,例如 s[i]=converted_number
。
I am creating a console program that asks for users to input integers between 0 and 100 and then outputs each number and how many times is was entered. Each number the user enters is separated by a space and input ends when 0 is entered. So far i am using cin.getline
to store the numbers in a character array. This limits the ability to manipulate the numbers as they are stored as ascii characters. If the user types in 10 i want to be able to store 10 into a new array as a single integer, such as s[i]=converted_number
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不用使用 cin.getline ,只需执行以下操作:
这将为您提供整数形式的输入。
Instead of using
cin.getline
simply do:That will get you the input in integer form.
我不确定这是否是您正在寻找的东西,但 boost 有一个很好的东西。它称为
lexical_cast
。请参阅http://www.boost.org/doc/libs/ 1_47_0/libs/conversion/lexical_cast.htmI'm not sure if this is what you are looking for but boost has a nice thing for that. It's called
lexical_cast
. See http://www.boost.org/doc/libs/1_47_0/libs/conversion/lexical_cast.htm