C ++矢量崩溃

时间:2014-06-10 22:35:12

标签: c++

全晚,

我的以下代码:

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

const char NEWLINE = 0x0D + 0x0A;

int main() {

  int headerCount;
  char response;
  int x = 0;
  int i = 0;
  string header[headerCount];

  cout << "How many columns appear in you file: ";
  cin >> headerCount;
  cout << "There are " << headerCount << " columns in your file? (Y/N): ";
  cin >> response;
  if(response == 'Y'){
    cout << endl << "Okay" << endl;

    ofstream myfile;
    myfile.open("example.csv", ios::binary | ios::out);
    string vals;
    vector< vector<string> > buff;

    // To set values
    while(i != 3){
      vector<string> temp;

      while(x != headerCount){
        if (i == 0){
          cout << "What is header number " << (x + 1) << endl;
        } else {
          cout << "What is person " << i << "'s " << header[x] << ":";
        }
        cin >> vals;
        if (i == 0) header[x] = vals;
        temp.push_back("\"" + vals + "\"");
        if (x != (headerCount - 1)) { temp.push_back(","); }
        x++;
      }
      buff.push_back(temp); // Store the array in the buffer
      x = 0;
      i++;
    }

    //To access values
    for(vector<vector<string> >::iterator it = buff.begin(); it != buff.end(); ++it){
      //it is now a pointer to a vector<int>
      for(vector<string>::iterator jt = it->begin(); jt != it->end(); ++jt){
        // jt is now a pointer to an integer.
        myfile << *jt;
      }
      myfile << "\r\n";
    }

    myfile.close();

  } else {
    cout << endl << "oh";
  }

  return 0;
}

第四次迭代崩溃。 例如。值[0] [4]

有什么想法吗?我对矢量来说相当新。此代码导致Code :: Blocks将我的应用程序显示为“无响应”。现在真的迷失了!

我知道可能有很多类似的帖子,但我已经在网上拖网了几个小时。

2 个答案:

答案 0 :(得分:0)

首先,你必须感谢@ 40two迅速发现主要问题。

所以添加缺少的包含。然后,将变量header定义为矢量,以允许动态调整大小。但是,在cin读数之后执行此操作,以确保变量已初始化且不是随机的:

  cin >> headerCount;                   // existing line 
  vector<string> header(headerCount);   // definition in a new place

然后它应该运行而不会崩溃(使用VC2013测试)

答案 1 :(得分:0)

正如@ 40提到的,真正的问题是你不能在C ++中使用可变大小的数组。有很大的空间来显着改进代码。为了让您快速前进,请参阅下面的修复代码。

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

const char NEWLINE = 0x0D + 0x0A;

int main() {

  int headerCount;
  char response;
  int x = 0;
  int i = 0;

  cout << "How many columns appear in you file: ";
  cin >> headerCount;
  cout << "There are " << headerCount << " columns in your file? (Y/N): ";
  cin >> response;
  if(response == 'Y'){
    cout << endl << "Okay" << endl;

    ofstream myfile;
    myfile.open("example.csv", ios::binary | ios::out);
    string vals;
    vector< vector<string> > buff;

    // To set values
    while(i != 3){
      vector<string> temp;

      while(x != headerCount)
      {
        string header;
        if (i == 0){
          cout << "What is header number " << (x + 1) << endl;
        } else {
          cout << "What is person " << i << "'s " << header << ":";
        }
        cin >> vals;
        if (i == 0)
            header = vals;
        temp.push_back("\"" + vals + "\"");
        if (x != (headerCount - 1)) { temp.push_back(","); }
        x++;
      }
      buff.push_back(temp); // Store the array in the buffer
      x = 0;
      i++;
    }

    //To access values
    for(vector<vector<string> >::iterator it = buff.begin(); it != buff.end(); ++it){
      //it is now a pointer to a vector<int>
      for(vector<string>::iterator jt = it->begin(); jt != it->end(); ++jt){
        // jt is now a pointer to an integer.
        myfile << *jt;
      }
      myfile << "\r\n";
    }

    myfile.close();

  } else {
    cout << endl << "oh";
  }

  return 0;
}