2017-04-12 117 views
0

過程中有錯誤,我有以下代碼編譯正確:可執行文件運行時

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include "INVESTMENT.h" 
#include <vector> 
using namespace std; 


int main() 
{ 


    ifstream inputFile("jroth.csv"); 
    string sHolder; //used as placeholder for string 
    float fHolder; //used as placeholder for float 
    double dHolder; ///used as placeholder for double 

// while (inputFile.good()) 
    vector<int> InvestVector; //will hold class (Investment) which contains string, double, and float 
    for (int i=0; i <= 7; i++) 
    { 
     Investment InvestVector[i]; //create new class for each line being pulled from .csv file 
     getline(inputFile, sHolder, ','); //pull in investment symbol as string 
     InvestVector[i].setSymbol(sHolder); //store string in the class 
     cout << InvestVector[i].getSymbol(); //verify string store properly 
    } 

    return 0; 
} 

只要程序運行,可執行崩潰。任何想法爲什麼?

+6

您的調試器以洞察力的方式提供了什麼?如果你是C++的新手,儘量避免使用'namespace std'這個壞習慣。 'std ::'前綴是故意的,它有助於避免與自己代碼中的名稱衝突。 – tadman

+0

此處未顯示「投資」是什麼。在那裏可能會發生許多事情。有什麼理由爲什麼頭文件在全部大寫? – tadman

+0

在for循環中,您使用的是*動態數組*,這不受C++標準支持。 –

回答

0

假設使用的是一個C++編譯器具有可變長度數組,這是不標準的C++,那麼這行:

Investment InvestVector[i]; 

創建具有「i」的地方的陣列,編號從0到I-1 。然後這兩行:

InvestVector[i].setSymbol(sHolder); 
cout << InvestVector[i].getSymbol(); 

將嘗試使用數組的位置編號「i」,即數組的末尾之一。