2014-04-15 2 views
-1
#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++ 함수를 이해하지 못하고 특히 함수의 구조체에 읽은 파일을 전달하는 방법을 알지 못합니다.

+1

몇 가지 : 코드를 올바르게 형식화하여 다른 사람들이 쉽게 읽을 수 있도록하십시오. 예를 들어, 함수 정의의'{...} '안의 코드는 일정한 수의 공백으로 들여 쓰기되어야합니다. 둘째,'getOutputFileName'과 같은 함수에서 호출자에게 뭔가를'반환 '한 다음 다른 행 ('outFile.close()')을 사용합니다. 그런 식으로 코드를 작성하지 마십시오. 일단 그것이 돌아 오면 그것이 그것이하는 마지막 일입니다. 컴파일러가 그것에 대해 불평 할 수 있습니다. 또한 이러한 함수에서 파일을 열고 닫아야하는지 잘 모르겠습니다. –

+0

파일 상단에'readStudents' 함수를 선언하여 두 개의 매개 변수 :'vector '와'string'을 취합니다.하지만 아래 함수를 구현할 때 매개 변수를'(vector inputFileName)'. 그것은 단 하나의 매개 변수이며 잘못된 이름입니다. 이 함수 내에서 Student 구조체를 만들어 벡터에 추가해야합니다. 그것이 그렇듯이, 당신은 들판을 읽고 나서 잊어 버리고 있습니다. 아무것도 벡터에 추가되지 않습니다. –

+0

오케이, 들여 쓰기 미안해, 나는 빨간 상자가 말한 것을했다고 생각했다. 내가 열어 본 파일을 항상 닫아야한다고 생각 했어. 구조를 통과하는 동안 while 루프가 올바른 것입니까? – user3527715

답변

0

오류는 내가 당신의 writeStudents 기능 같아요

... 4 기능에서 초기화되지 않은 변수입니다.

몇 가지 문제가 있습니다.


이 함수에는 선언 및 정의가 있습니다. 이것들은 호환 가능해야합니다, 그들은 그렇게하지 않습니다 :

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

선언은 함수가 2 개의 매개 변수를 얻는다 고 말합니다; 정의는 동일해야한다; 또한이 매개 변수에 이름을 지정해야합니다. 여기에 예제 정의는 다음과 같습니다

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

parameter1parameter2보다 더 나은 이름이있다 - 당신은 이미 다른 곳이 더 좋은 이름을 코드에 있습니다

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

쓸모없는 혼란 코드가있다 writeStudents 함수에서 :

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

실수로 어딘가에서 복사했다고 생각합니다. 컴파일러는 그것을 이해하기 위해 최선을 다했지만 혼란 스러웠습니다. 이 코드를 삭제하십시오.


나머지는 쉬워야합니다.