【问题标题】:c++ ending loop with 'enter' hitc++ 用'enter'命中结束循环
【发布时间】:2019-04-22 06:10:09
【问题描述】:

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

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

【问题讨论】:

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

标签: c++ arrays input iostream


【解决方案1】:

问题

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

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

解决方案

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

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

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

其他解决方案(次优)

如果是为了学校而不允许使用矢量,则可以选择次优的替代品:

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

这会估计字符串中可能存在的最大数字数(您肯定会更少)。

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案3】:

      你的代码中的问题是你已经分配了足够的空间来容纳一个int

      int *tab=new int;
      

      并且正在使用tab,就好像它可以根据需要容纳尽可能多的int

      如果允许您使用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::vector,您将不得不弄清楚如何处理tab 的动态特性。作为一种快速解决方法,您可以使用静态定义的数组并在用完数组容量后立即停止读取。

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

      【讨论】:

        猜你喜欢
        • 2014-04-20
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 2016-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多