C++ noobc++;我不了解函数,尤其是如何将读取的文件传递到函数的结构中 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 字符串getInputFileName(); 字符串getOutputFileName(); void readStudents(向量、字符串); void writeStudents(向量、字符串); int main(){ 矢量学生; 字符串输入文件名,输出文件名; inputFileName=getInputFileName(); outputFileName=getOutputFileName(); readStudents(学生,输入文件名); 编写学生(学生,输出文件名); 系统(“暂停”); 返回0; } 字符串getInputFileName(无效){ 字符串位置; 无法定位; 返回位置; } 字符串getOutputFileName(无效){ 字符串写位置; 不能写位置; 返回写位置; } void readStudents(矢量输入文件名){ 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 矢量学生; 字符串名; 二年级一、二、三年级; 河流充填; open(getInputFileName()); 而(!infle.eof()){ 填充>>名称>>一级>>二级>>三级; } } //从输入文件写入 void writeStudents(向量输出文件名){ 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 矢量学生; 字符串名; 二年级一、二、三年级; 出流孔的直径; open(getInputFileName()); 而(!outFile.eof()){ 外锉

C++ noobc++;我不了解函数,尤其是如何将读取的文件传递到函数的结构中 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 字符串getInputFileName(); 字符串getOutputFileName(); void readStudents(向量、字符串); void writeStudents(向量、字符串); int main(){ 矢量学生; 字符串输入文件名,输出文件名; inputFileName=getInputFileName(); outputFileName=getOutputFileName(); readStudents(学生,输入文件名); 编写学生(学生,输出文件名); 系统(“暂停”); 返回0; } 字符串getInputFileName(无效){ 字符串位置; 无法定位; 返回位置; } 字符串getOutputFileName(无效){ 字符串写位置; 不能写位置; 返回写位置; } void readStudents(矢量输入文件名){ 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 矢量学生; 字符串名; 二年级一、二、三年级; 河流充填; open(getInputFileName()); 而(!infle.eof()){ 填充>>名称>>一级>>二级>>三级; } } //从输入文件写入 void writeStudents(向量输出文件名){ 结构学生{ 字符串名称、状态; 二年级,一年级,二年级, 三年级,平均分; }; 矢量学生; 字符串名; 二年级一、二、三年级; 出流孔的直径; open(getInputFileName()); 而(!outFile.eof()){ 外锉,c++,function,structure,fstream,C++,Function,Structure,Fstream,我得到的错误是第四个函数中未初始化的变量 我想这是你的writeStudents函数 有几件事不对 此函数有一个声明和一个定义。它们应该兼容,但不完全兼容: #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <vector> #include <cstdlib> using namespace std;

我得到的错误是第四个函数中未初始化的变量

我想这是你的
writeStudents
函数

有几件事不对


此函数有一个声明和一个定义。它们应该兼容,但不完全兼容:

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

using namespace std;


struct Student{
    string name, status;
    double grade1, grade2, 
           grade3, average;
};

string getInputFileName();
string getOutputFileName();
void readStudents(vector<Student>, string);
void writeStudents(vector<Student>, string);

int main(){

    vector<Student> students;
    string inputFileName, outputFileName;

    inputFileName = getInputFileName();
    outputFileName = getOutputFileName();

    readStudents(students, inputFileName);
    writeStudents(students, outputFileName);

    system("pause");
    return 0;
}

string getInputFileName(void){

    string getLocation;
    cout << "Please type the location of the file"
         << " you would like to open." << endl
         << ">>";
    cin  >> getLocation;


    return getLocation;

}

string getOutputFileName(void){


    string writeLocation;
    cout << "Please type the location of the file"
         << " you would like to write to." << endl
             << ">>";
    cin  >> writeLocation;


    return writeLocation;


}

void readStudents(vector<struct Student> inputFileName){
    struct Student{
    string name, status;
    double grade1, grade2, 
       grade3, average;
    };
    vector<Student> students;
    string name;
    double grade1, grade2, grade3;
    ifstream inFile;
    inFile.open(getInputFileName());

    while(!inFile.eof()){
            inFile >> name >> grade1 >> grade2 >> grade3;
    }
}
//write from input file 
void writeStudents(vector<struct Student> outputFileName){

    struct Student{
    string name, status;
    double grade1, grade2, 
       grade3, average;
    };
    vector<Student> students;
    string name;
    double grade1, grade2, grade3;
    ofstream outFile;
    outFile.open(getInputFileName());

    while(!outFile.eof()){
            outFile << name << grade1 << grade2 << grade3;
    }
}

在您的
writeStudents
函数中有许多无用且令人困惑的代码:

void writeStudents(vector<Student> students, string outputFileName) {...}
// equivalent definition; works the same way but easier to read
struct学生{
字符串名称、状态;
二年级,一年级,二年级,
三年级,平均分;
};
矢量学生;
字符串名;
二年级一、二、三年级;
我猜您是从某个地方错误地复制了它;编译器尽了最大的努力来理解它,但可能弄错了。只需删除此代码



剩下的应该很简单。

有几件事:尝试正确格式化代码,以便其他人更容易阅读。例如,
{…}中的代码函数定义的
应以一致的空格数缩进。其次,在函数中,如
getOutputFileName
向调用者返回某个内容,然后有另一行(
outFile.close()
)。不要这样编写代码。一旦它返回,这是它最后做的事情。编译器可能会对此抱怨。此外,我不确定您是否需要在这些函数中打开和关闭文件。您可以在文件顶部声明函数
readStudents
,以获取两个参数:向量
和字符串
但是在实现下面的函数时,将参数更改为
(矢量输入文件名)
。这只是一个参数,它的名称不正确。在该函数中,您需要创建Student结构并将其添加到向量中。事实上,您正在读取字段,然后忘记了它们;向量中没有添加任何内容。好的,很抱歉缩进,我以为我做了红色框所说的事情。好的,我以为我一直在做eded关闭我打开的文件。while循环在通过结构时是否正确?是的,如果打开文件,则应关闭它。但是在该函数中,您也不需要打开它。您打开文件是为了读取或写入它。但是在这些函数中,您没有读取或写入,因此无需打开和关闭文件。最后首先,如果您确实需要关闭某些内容,您可以在
return
语句之前关闭它。我看到您确实在表单中对其进行了缩进,但是stackoverflow被制表符和空格的混合弄糊涂了。我将其转换为所有空格字符。您的
while
循环看起来不错,但是您的短语“通过结构传递它”在我的第二个评论中,我提到了这两个函数的问题。
void writeStudents(vector<Student> parameter1, string parameter2) {...}
void writeStudents(vector<Student> students, string outputFileName) {...}
// equivalent definition; works the same way but easier to read
struct Student{
string name, status;
double grade1, grade2, 
   grade3, average;
};
vector<Student> students;
string name;
double grade1, grade2, grade3;