简体   繁体   English

C ++以'enter'命中结尾循环

[英]c++ ending loop with 'enter' hit

I need to end the input loop by hitting the enter key. 我需要通过按Enter键来结束输入循环。 Tried to find something, and I got some guy here saying that this code below will work, sadly it doesn't. 试图找到一些东西,我在这里找一个人说下面的这段代码可以工作,可惜的是没有。 What's wrong? 怎么了?

#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;
}

Go it, thanks! 去吧,谢谢!

Here's the code: 这是代码:

#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;
}

The problem 问题

In your code, you have an allocation problem, since you allocate a single integer for tab. 在代码中,您会遇到分配问题,因为您为tab分配了一个整数。 So as soon as you have read the first number, you go out of bounds. 因此,一旦您阅读了第一个数字,您就会越界。 This is undefined behavior. 这是未定义的行为。

In addition, your outer while is designed to loop until an empty line is entered, with no numbers. 此外,您的while while循环播放直到输入空行(无数字)为止。

The solution 解决方案

If your concern is to read a couple of numbers on a single line, then there's no need for a loop: 如果您要在一行上读取几个数字,则无需循环:

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

This approach uses a vector. 这种方法使用向量。 This has the advantage that you don't need to know how much numbers you'll have in the end. 这样的好处是,您不需要知道最后会有多少个数字。 Afterwards, you can find out the size of the vector with tab.size() and you can access single elements exactly as you would for an array. 然后,您可以使用tab.size()找出向量的大小,并且可以完全像访问数组一样访问单个元素。

Other solution (suboptimal) 其他解决方案(次优)

If it's for school an you're not allowed to use vector, you can go for a suboptimal substitute: 如果是学校,则不允许使用向量,则可以选择次优替代品:

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

This makes an estimate of the maximum number of numbers that could potentially be in the string (you'll certainly have less). 这将估计可能在字符串中的最大数字数(您肯定会更少)。

One way to do it is the following, which reads numbers in that are separated by spaces and puts them in a vector. 一种方法是使用以下方法,该方法读取以空格分隔的数字并将它们放入向量中。

#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;
  }
  */
}

Example input: 输入示例:

32 22 62 723765 26 62 72 7 -5 7 2 7 152 62 6 262 72

The problem in your code is that you have allocated enough space to hold one int in 代码中的问题是您分配了足够的空间来容纳一个int

int *tab=new int;

and are using tab as though it can hold as many int s as you need. 并且正在使用tab ,好像它可以容纳所需的int一样多。

If you are allowed to use std::vector , change the above line to: 如果允许使用std::vector ,则将上面的行更改为:

std::vector<int> tab;

and use 和使用

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

If you are not allowed to use std::vector , you'll have to figure out how to deal with the dynamic nature of tab . 如果不允许使用std::vector ,则必须弄清楚如何处理tab的动态性质。 As a quick work around, you can use a statically defined array and stop reading as soon as you have used up the capacity of the array. 为了快速解决,可以使用静态定义的数组,并在耗尽数组容量后立即停止读取。

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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM