#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;
}
}
오류는 네 번째 함수에서 초기화되지 않은 변수입니다. 나는 이유를 모른다. 아마도 정보를 올바르게 입력하지 않았기 때문일 수 있습니다. 구조체 데이터 형식을 사용하는 동안 사용자 입력 파일 위치에서 읽고 사용자가 지정한 위치에 쓸 수 있어야합니다. 지금 당장 붙어있어. 내가 갖는Noob C++ 함수를 이해하지 못하고 특히 함수의 구조체에 읽은 파일을 전달하는 방법을 알지 못합니다.
몇 가지 : 코드를 올바르게 형식화하여 다른 사람들이 쉽게 읽을 수 있도록하십시오. 예를 들어, 함수 정의의'{...} '안의 코드는 일정한 수의 공백으로 들여 쓰기되어야합니다. 둘째,'getOutputFileName'과 같은 함수에서 호출자에게 뭔가를'반환 '한 다음 다른 행 ('outFile.close()')을 사용합니다. 그런 식으로 코드를 작성하지 마십시오. 일단 그것이 돌아 오면 그것이 그것이하는 마지막 일입니다. 컴파일러가 그것에 대해 불평 할 수 있습니다. 또한 이러한 함수에서 파일을 열고 닫아야하는지 잘 모르겠습니다. –
파일 상단에'readStudents' 함수를 선언하여 두 개의 매개 변수 :'vector'와'string'을 취합니다.하지만 아래 함수를 구현할 때 매개 변수를'(vector inputFileName)'. 그것은 단 하나의 매개 변수이며 잘못된 이름입니다. 이 함수 내에서 Student 구조체를 만들어 벡터에 추가해야합니다. 그것이 그렇듯이, 당신은 들판을 읽고 나서 잊어 버리고 있습니다. 아무것도 벡터에 추가되지 않습니다. –
오케이, 들여 쓰기 미안해, 나는 빨간 상자가 말한 것을했다고 생각했다. 내가 열어 본 파일을 항상 닫아야한다고 생각 했어. 구조를 통과하는 동안 while 루프가 올바른 것입니까? – user3527715