用cin读取可变数量的整数 [英] Reading in a variable number of integers with cin

查看:95
本文介绍了用cin读取可变数量的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我写的东西写一个驱动程序,但是不记得如何使用这个非常简单的东西。我现在的代码:


int myArr [100];

int temp;

int ind = 0;

cout<< 输入一些整数:;

while(cin>> temp)

{

myArr [ind ++] = temp;

}


问题是在用户输入非号码之前它不会退出。

赢了如果用户只是点击进入就退出。


我想要创建的是允许用户用空格分隔
值的东西,完成输入后按Enter键。一个

可变数量的项目将用空格分隔,例如:

"输入要用它们之间的空格排序的整数和Enter to

完成"

I''m writing a driver for something I wrote, and can''t remember how to
do this very simple thing. The code I have at this point in time:

int myArr[100];
int temp;
int ind = 0;
cout << "enter some integers: ";
while (cin >> temp)
{
myArr[ind++] = temp;
}

The problem is that it doesn''t exit until the user enters a non-number.
It won''t exit if the user simply hits enter.

What I''m trying to create is something that allows the user to separate
values by spaces, and press the Enter key when done entering them. A
variable number of items will be separated by spaces, e.g.:
"Enter the integers to be sorted with spaces between them and Enter to
finish"

推荐答案



H.写道:

H. wrote:
I我正在为我写的东西写一个驱动程序,并且不记得如何做这个非常简单的事情。我现在的代码:


您发布的代码无法编译。

请确保您在此处发布可编译代码想要帮助。

int myArr [100];


你怎么知道100是一个很好的价值?如果我想输入101

数字怎么办?

使用std :: vector代替保持你的选项开放。

int temp;


始终初始化变量。

int ind = 0;
cout<< 输入一些整数:;
而(cin>> temp)
{
myArr [ind ++] = temp;


在索引101处,这会打击;)

}

问题是它不会退出,直到用户输入一个非数字。
如果用户只是点击进入,它就不会退出。

我想要创建的是允许用户分开的内容/>按空格键,输入完成后按Enter键。可变数量的项目将用空格分隔,例如:
输入要用它们之间的空格排序的整数,并输入以完成
I''m writing a driver for something I wrote, and can''t remember how to
do this very simple thing. The code I have at this point in time:
The code you posted doesn''t compile.
Please make sure you post compilable code here if you want help.
int myArr[100];
How do you know 100 is a good value? What if I want to enter 101
numbers?
Use a std::vector instead to keep your options open.
int temp;
Always initialize your variables.
int ind = 0;
cout << "enter some integers: ";
while (cin >> temp)
{
myArr[ind++] = temp;
At index 101, this will blow ;)
}

The problem is that it doesn''t exit until the user enters a non-number.
It won''t exit if the user simply hits enter.

What I''m trying to create is something that allows the user to separate
values by spaces, and press the Enter key when done entering them. A
variable number of items will be separated by spaces, e.g.:
"Enter the integers to be sorted with spaces between them and Enter to
finish"




我可能在这里为你做功课,但我希望

排序算法是真正的挑战。


这里有一些代码供您使用:

#include< iostream> // std :: cout,std :: endl

#include< string> // std :: string

#include< sstream> // std :: stringstream

#include< vector> // std :: vector

#include< algorithm> // std :: copy

#include< iterator> // std :: ostream_iterator

int main()

{

std :: cout<< 输入用空格分隔的整数:\ n" ;;


std :: string line;

if(std :: getline(std :: cin) ,行))

{

std :: stringstream ss(line);

std :: vector< long>值;

长l = 0;


而(ss>> l)

values.push_back(l) ;


if(values.size())

std :: copy(values.begin(),values.end(),

std :: ostream_iterator< long>(std :: cout," \ n"));

}

}


干杯,

Andre



I''m probably doing your homework for you here, but I''ll hope that the
sorting algorithm is the real challenge here.

Here''s some code for you to work with:
#include <iostream> // std::cout, std::endl
#include <string> // std::string
#include <sstream> // std::stringstream
#include <vector> // std::vector
#include <algorithm> // std::copy
#include <iterator> // std::ostream_iterator

int main()
{
std::cout << "Enter integers separated by spaces:\n";

std::string line;
if ( std::getline( std::cin, line ))
{
std::stringstream ss( line );
std::vector<long> values;
long l = 0;

while( ss >> l )
values.push_back( l );

if ( values.size() )
std::copy( values.begin(), values.end(),
std::ostream_iterator<long>(std::cout, "\n"));
}
}

Cheers,
Andre


感谢所有这一切,它非常有教育意义。 (不幸的是,对于我正在尝试做的事情来说,这是一个过度杀戮。但是再次,谢谢。)向量

很棒;它们不能用于我正在做的事情,需要阵列。)

并且将阵列编号硬编码为100对于测试驱动程序来说是好的
$ b $我正在写作;永远不会有101个号码。基本上我们的目标是创造一些尽可能简单的东西来完成工作,除了iostream之外没有额外的额外课程。它被用作

家庭作业问题的驱动程序,但我已经创建了一个更简单的驱动程序,很难

编码条目数,这对于分配来说已经足够了在

手,因为赋值不是关于数字,而是以递归的给定方式操纵它们。实际上,对于驱动程序,我可以完全跳过用户输入,只需输入测试列表的所有递归函数,所以从这个意义上说,它''''''''''''''''''''''''''''''''''''''''''''''''''''''''这不是一个家庭作业问题

all,只是一个for my edification"问题。我会搞清楚的,因为

我确定它真的不是那么难做的事。

在***** @ gmail.com 中写道:
Thanks for all that, it was very educational. (Unfortunately, It''s an
overkill for what I''m trying to do, but again, thanks though.) Vectors
are great; they can''t be used for what I''m doing, arrays are needed.)
And hard-coding the array number to 100 is fine for the testing driver
I''m writing; there will never be 101 numbers. Basically the goal is to
create something as simple as possible to get the job done, with no
extra classes besides iostream. It is being used as a driver for a
homework problem, but I already created a simpler driver that hard
codes the number of entries, which is good enough for the assignment at
hand, since the assignment is not about the numbers but manipulating
them in a given way with recursion. Actually, for the driver, I could
skip the user input altogether and just feed the recursive function all
of the test lists, and so in that sense, it''s not a homework problem at
all, just a "for my edification" problem. I''ll figure it out, because
I''m sure it can''t really be that hard a thing to do.

in*****@gmail.com wrote:
H.写道:
I我正在为我写的东西写一个驱动程序,并且不记得如何做这个非常简单的事情。我现在的代码:
I''m writing a driver for something I wrote, and can''t remember how to
do this very simple thing. The code I have at this point in time:



您发布的代码无法编译。
如果您需要帮助,请确保在此处发布可编译的代码。< br>



The code you posted doesn''t compile.
Please make sure you post compilable code here if you want help.

int myArr [100];
int myArr[100];



你怎么知道100是一个很好的价值?如果我想输入101
数字怎么办?
使用std :: vector代替保持选项打开。



How do you know 100 is a good value? What if I want to enter 101
numbers?
Use a std::vector instead to keep your options open.

int temp;
int temp;



总是初始化你的变量。



Always initialize your variables.

int ind = 0;
cout<< 输入一些整数:;
while(cin>> temp)
{
myArr [ind ++] = temp;
int ind = 0;
cout << "enter some integers: ";
while (cin >> temp)
{
myArr[ind++] = temp;



在索引101,这将打击;)



At index 101, this will blow ;)

}

问题是,在用户输入非数字之前它不会退出。<如果用户只是点击进入,它就不会退出。

我想要创建的是允许用户用空格分隔值的东西,然后按完成输入后输入密钥。可变数量的项目将用空格分隔,例如:
输入要用它们之间的空格排序的整数,并输入以完成
}

The problem is that it doesn''t exit until the user enters a non-number.
It won''t exit if the user simply hits enter.

What I''m trying to create is something that allows the user to separate
values by spaces, and press the Enter key when done entering them. A
variable number of items will be separated by spaces, e.g.:
"Enter the integers to be sorted with spaces between them and Enter to
finish"



我可能在这里为你做功课,但我希望
排序算法是真正的挑战。

这里有一些代码供您使用:
#include< iostream> // std :: cout,std :: endl
#include< string> // std :: string
#include< sstream> // std :: stringstream
#include< vector> // std :: vector
#include< algorithm> // std :: copy
#include< iterator> // std :: ostream_iterator


{
std :: cout<< 输入用空格分隔的整数:\ n;

std :: string line;
if(std :: getline(std :: cin,line))
{
std :: stringstream ss(line);
std :: vector< long>值;
long l = 0;

while(ss>> l)
values.push_back(l);

if(values) .size())
std :: copy(values.begin(),values.end(),
std :: ostream_iterator< long>(std :: cout," \ n") );
}

干杯,
安德烈



I''m probably doing your homework for you here, but I''ll hope that the
sorting algorithm is the real challenge here.

Here''s some code for you to work with:
#include <iostream> // std::cout, std::endl
#include <string> // std::string
#include <sstream> // std::stringstream
#include <vector> // std::vector
#include <algorithm> // std::copy
#include <iterator> // std::ostream_iterator

int main()
{
std::cout << "Enter integers separated by spaces:\n";

std::string line;
if ( std::getline( std::cin, line ))
{
std::stringstream ss( line );
std::vector<long> values;
long l = 0;

while( ss >> l )
values.push_back( l );

if ( values.size() )
std::copy( values.begin(), values.end(),
std::ostream_iterator<long>(std::cout, "\n"));
}
}

Cheers,
Andre






我不确定我是否理解你想要什么,但我认为在unix终端上输入Ctrl-d

应该结束while循环。


这是什么你想要吗?

H.写道:
I am not sure if I understand what you want but I think typing Ctrl-d
on the unix terminal should end the while loop.

Is that what you want?
H. wrote:
我正在为我写的东西写一个驱动程序,并且不记得如何
做这个非常简单的事情。我现在的代码:

int myArr [100];
int temp;
int ind = 0;
cout<< 输入一些整数:;
while(cin>> temp)
{
myArr [ind ++] = temp;
}
问题是在用户输入非号码之前它不会退出。
如果用户只是点击进入它就不会退出。

我在做什么尝试创建是允许用户按空格分隔值的东西,并在完成输入后按Enter键。可变数量的项目将用空格分隔,例如:
输入要用它们之间的空格排序的整数,并输入以完成
I''m writing a driver for something I wrote, and can''t remember how to
do this very simple thing. The code I have at this point in time:

int myArr[100];
int temp;
int ind = 0;
cout << "enter some integers: ";
while (cin >> temp)
{
myArr[ind++] = temp;
}

The problem is that it doesn''t exit until the user enters a non-number.
It won''t exit if the user simply hits enter.

What I''m trying to create is something that allows the user to separate
values by spaces, and press the Enter key when done entering them. A
variable number of items will be separated by spaces, e.g.:
"Enter the integers to be sorted with spaces between them and Enter to
finish"






这篇关于用cin读取可变数量的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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