2012-04-09 61 views
0

我很新的編程,需要一些幫助,謝謝!我需要通過一個ifstream到一個函數,所以我可以將它添加到一個向量

我有一個名爲namelist.txt的文本文件。我需要將inFile傳遞給一個函數,該函數可以將名稱列表(以firstname lastname的格式,例如:chuck norris)添加到名爲vecStudent的矢量中。你如何將文件傳遞給一個函數,如果可能的話,我如何將這些名稱添加到一個矢量中?

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

using namespace std; 

ifstream readtoVec(); //function prototypes 
int displayAll(); 
int add(); 
int remove(); 
int savequit(); 

int main() 
{ 
char cInput; 
string strFileName; 
vector<string> vecStudent; 
ifstream inFile; 
ofstream outFile; 

{ 
    cout << "Please Enter the data file name (with location): "; 
    cin >> strFileName; 

    inFile.open(strFileName.c_str()); 

    if (inFile.fail()) 

    { 
     cout << "Input file error!" << endl; 

     return -1; 
    } 

    // call a function to read the contents of the input file into vecStudent 
    else 
    { 
     readtoVec (ifstream& inFile); 

我收到這裏所說的錯誤 「類型名稱是不允許的」

} 



while (true) 
{ 
    cout << "--------------------------------------" << endl; 
    cout << " Student Record - Main Menu " << endl; 
    cout << "--------------------------------------" << endl; 
    cout << " Enter 1 to display ALL students " << endl; 
    cout << " Enter 2 to add a student name " << endl; 
    cout << " Enter 3 to delete a student name " << endl; 
    cout << " Enter 4 to SAVE and quit the program " << endl; 
    cout << "--------------------------------------" << endl; 
    cout << " Enter menu option: " ; 
    cin >> cInput; 
    switch (cInput) 
    { 
     case '1': 
      //call function 
      break; 
     case '2': 
      //call function 
      break; 
     case '3': 
      //call function 
      break; 
     case '4': 
      //call function 
      return 0; 
     default: 
      cout << "invalid input" << endl; 
      break; 
    } 

return 0; 
} 

回答

1

ifstream的readtoVec(); //函數原型

這是說readtoVec()返回ifstream,並且不帶任何參數,但這裏:

readtoVec(ifstream的& INFILE);

當你打電話給readtoVec時,你(有點)試圖傳遞一個參數。至少根據你想怎麼稱呼它,聲明應該像void readtoVec(ifstream &);

當你打電話,你做雖然指定參數的類型,因此呼叫將如下所示: readtoVec(inFile);

+0

感謝您的回覆,我將如何將.txt文件中的名稱列表添加到函數readtoVec中的向量?我認爲虛空不會返回任何東西,然後主要。 – 2012-04-09 19:54:16

相關問題