我是编程新手,需要一些帮助,谢谢!
我有一个名为namelist.txt的文本文件。我需要将inFile传递给一个函数,该函数可以将名称列表(格式为firstname lastname,ex:chuck norris)添加到名为vecStudent的向量中。如何将inFile传递给函数,如果可能的话,我如何将名称添加到向量中?
#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;
}
答案 0 :(得分:1)
ifstream readtoVec(); //函数原型
这表示readtoVec()
会返回ifstream
,并且不会在此处采取任何参数:
readtoVec(ifstream&amp; inFile);
你打电话给readtoVec
时试图传递一个参数。至少根据您尝试调用它的方式,声明应该类似于void readtoVec(ifstream &);
当您拨打电话时,不指定参数类型,因此调用如下:readtoVec(inFile);