点击“Enter”的 C++ 结束循环

c++ ending loop with 'enter' hit

提问人:Krystian Niżeniec 提问时间:11/20/2018 最后编辑:Krystian Niżeniec 更新时间:11/20/2018 访问量:272

问:

我需要通过按回车键来结束输入循环。试图找到一些东西,我在这里有人说下面的代码可以工作,可惜它没有。怎么了?

#include <iostream>
#include <sstream>

using namespace std;
int main() {
    int a = 0, h = 0, i=0;
    string line;
    int *tab=new int;
    while (getline(cin, line) && line.length() > 0) // line not empty
    {
        stringstream linestr(line);
        while (linestr >> a)// recommend better checking here. Look up std::strtol
        {
           tab[i]=a;
        }
    i++;
    }


    return 0;
}

去吧,谢谢!

代码如下:

#include <iostream>
#include <sstream>
using namespace std;
int main() {
    int a = 0, i=0;
    string line;
    getline(cin, line);
    stringstream linestr(line);
    int *tab = new int[line.size()/2+1]; 
    while ( linestr >> a ) 
    {
        tab[i]=a;
        i++;
    }

    for(int j=0; j<i; j++) cout<<tab[j]<<" ";
    return 0;
}
C++ 数组 输入 IOSTREAM

评论

0赞 Rivasa 11/20/2018
我很担心,为什么在这种情况下需要循环?如果你要按回车键,那么你可以简单地使用一个输入流来获取一个字符串并完成它。
4赞 Cory Kramer 11/20/2018
int *tab=new int;然后。。。。哦,天哪,你快要过得很糟糕了tab[i]=a;
0赞 Krystian Niżeniec 11/20/2018
@Rivasa 我需要将整数输入到动态数组中,直到我按回车键。我以为循环就是方法。你的意思是我可以输入一个带空格的 stwing,然后将其转换为整数?我不明白,可悲的是;x
0赞 Rivasa 11/20/2018
你不能那样做,你怎么知道一个数字什么时候结束?最好检查是否有空输入。
0赞 Krystian Niżeniec 11/20/2018
@Rivasa 认为 line.length() == 0 会起作用。那我该怎么办呢?

答:

2赞 Christophe 11/20/2018 #1

问题

在代码中,您遇到了分配问题,因为您为 tab 分配了一个整数。因此,一旦你读完第一个数字,你就出界了。这是未定义的行为。

此外,您的外部 while 被设计为循环,直到输入一个空行,没有数字。

解决方案

如果您关心的是读取一行上的几个数字,那么就不需要循环:

getline(cin, line);
stringstream linestr(line);
vector<int> tab; 
while ( linestr >> a ) 
{
    tab.push_back(a);
}

此方法使用向量。这样做的好处是,你不需要知道你最终会有多少数字。之后,您可以找出向量的大小,并且可以像访问数组一样访问单个元素。tab.size()

其他解决方案(次优)

如果是学校,你不允许使用vector,你可以选择次优的替代品:

int *tab = new int[line.size()/2+1];   

这样可以估计字符串中可能包含的最大数字数(您肯定会更少)。

1赞 Rivasa 11/20/2018 #2

一种方法是以下方法,它读取由空格分隔的数字并将它们放在向量中。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main() {
  std::string line;
  std::vector<int> v;
  std::getline(std::cin, line);
  std::stringstream sstream(line); 
  int i;
  while (sstream >> i) {
    v.push_back(i);
  }
  // Check your input vector.
  /*
  for(auto i : v){
    std::cout << i << std::endl;
  }
  */
}

输入示例:

32 22 62 723765 26 62 72 7 -5 7 2 7 152 62 6 262 72
1赞 R Sahu 11/20/2018 #3

代码中的问题在于,您已经分配了足够的空间来容纳一个int

int *tab=new int;

并且使用时,就好像它可以容纳您需要的任意数量的 s 一样。tabint

如果允许使用 ,请将上面的行更改为:std::vector

std::vector<int> tab;

并使用

while (getline(cin, line) && line.length() > 0) // line not empty
{
    stringstream linestr(line);
    while (linestr >> a)
    {
       tab.push_back(a);
    }
}

如果你不被允许使用 ,你就必须弄清楚如何处理 的动态性质。作为快速解决方法,您可以使用静态定义的数组,并在用完数组容量后立即停止读取。std::vectortab

int const MAX_ELEMENTS = 100;
int tab[MAX_ELEMENTS];

...

while (getline(cin, line) && line.length() > 0 && i < MAX_ELEMENTS)
{
    stringstream linestr(line);
    while (linestr >> a)
    {
       tab[i] = a;
       ++i;        // Needs to be here not outside this loop.
    }
}