[英]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:有比
parameter1
和parameter2
更好的名称 - 您在代码的其他地方已经有了这些更好的名称:
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.