简体   繁体   English

菜鸟 C++ 我不退出理解函数,尤其是如何将读取的文件传递到函数中的结构中

[英]Noob C++ I dont quit understand functions and especially how to pass the read file into the struct in a function

#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;
    }
}

The errors I'm getting are uninitialized variables in the 4th function.我得到的错误是第四个函数中的未初始化变量。 I don't know why.我不知道为什么。 probably because I'm not inputting the info correctly.可能是因为我没有正确输入信息。 I want to be able to read from the user input file location and write to the user specified location while using the structure data type.我希望能够在使用结构数据类型时从用户输入文件位置读取并写入用户指定的位置。 Im stuck at the moment.我现在卡住了。

The errors I'm getting are uninitialized variables in the 4th function...我得到的错误是第四个函数中未初始化的变量......

I guess it's your writeStudents function.我想这是你的writeStudents函数。

There are a few things wrong.有几件事是错误的。


This function has a declaration and a definition.这个函数有一个声明和一个定义。 These should be compatible, they aren't quite so:这些应该是兼容的,但并非如此:

void writeStudents(vector<Student>, string); // declaration
void writeStudents(vector<struct Student> outputFileName) {...} //definition

The declaration says that the functions gets 2 parameters;声明说这些函数有 2 个参数; the definition should state the same;定义应说明相同; and it should also give names to these parameters.并且它还应该为这些参数命名。 Here is an example definition:这是一个示例定义:

void writeStudents(vector<Student> parameter1, string parameter2) {...}

There are names better than parameter1 and parameter2 - you already have these better names elsewhere in your code:有比parameter1parameter2更好的名称 - 您在代码的其他地方已经有了这些更好的名称:

void writeStudents(vector<Student> students, string outputFileName) {...}
// equivalent definition; works the same way but easier to read

There is much useless and confusing code in your writeStudents function:您的writeStudents函数中有很多无用且令人困惑的代码:

struct Student{
string name, status;
double grade1, grade2, 
   grade3, average;
};
vector<Student> students;
string name;
double grade1, grade2, grade3;

I guess you copied it from somewhere by mistake;我猜你是从某个地方错误地复制了它; the compiler tried its best to make some sense of it, but probably got confused.编译器尽力理解它,但可能会感到困惑。 Just delete this code.只需删除此代码。


The rest should be easy.剩下的应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中的函数中传递只读向量? - How should I pass read only vectors in functions in C++? C ++将结构传递给函数 - c++ pass struct to a function SWIG (Java):如何将带有回调函数的结构从 Android 应用程序传递给 C++? - SWIG (Java): How do I pass a struct with callback functions to C++ from an Android application? 如何将结构从 c++ 传递到 c 中的 function? - How to pass struct from c++ to function in c? 如何将加载的结构数组传递给多个文件中的其他函数(C ++) - How to pass loaded struct array to other function in multiple file (c++) 如何打开文件,读取9个单独的字母,然后将它们全部传递给另一个函数? 我在数组中挣扎! (C ++) - How could I open a file, read 9 seperate letters, and pass them all to another function? I struggle with arrays! (c++) 如何在C ++中将结构写入文件并读取文件? - How to write a struct to file in C++ and read the file? C ++新手:我需要帮助来创建一个简单的计时器函数 - C++ noob: I need help creating a simple timer function 我不明白这段代码如何对这个 python 列表()进行排序,以及如何用向量在 C++ 中重现它 - I dont understand how this code sort this python list(), and how to reproduce it in C++ with a vector C++ 学校代码我不明白会发生什么或如何使用枚举 - C++ school code I dont understand what to expect or how to use enum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM