2016-04-27 69 views
-1

我已經更改了代碼,現在程序正確讀取了te文件形式的students.txt,但仍然是從用戶輸入而不是學生計算的。 TXT
的#include 的#include 的#include 的#include 的#include 的#include 的#include 的#include從文本文件讀入一個類中的對象 - 在另一個txt文件中寫入

using namespace std; 

//Declaring class Student 

class Student 
{ 
private: 

    string newName; 
    int newScore1; 
    int newScore2; 
    int newScore3; 
    float newFinal; 

public: 
    // No argument constructors 
    Student() 

{ 
    newName  = " "; 
    newScore1 = 0; 
    newScore2 = 0; 
    newScore3 = 0; 
    newFinal = 0.00; 

} 
//all arguements constructors 
Student (string name, int score1, int score2, int score3, float final) 

{ 
newName  = name; 
newScore1 = score1; 
newScore2 = score2; 
newScore3 = score3; 
newFinal  = final; 
} 
//getters 
string getName() const 
{ 
    return newName; 
} 
int getScore1() const 
{ 
return newScore1; 
} 
int getScore2()const 
{ 
return newScore2; 
} 
int getScore3()const 
{ 
return newScore3; 
} 

float getFinal() const 
{ 
    return newFinal; 
} 

//Setters 
void setName(string name) 
{ 
    newName = name; 
} 
void setScore1(int score1) 
{ 
newScore1=score1; 
} 
void setScore2(int score2) 
{ 
    newScore2=score2; 
} 
void setScore3 (int score3) 
{ 
newScore3 = score3; 
} 

void setFinal (float final) 
{ 
    newFinal = final; 
} 


}; 
//asks for number of students, 
// function asks for input to fill in vector 
//sorts the inputs to get max 2 scores out of 3 
//puts the data in a vector of class Student 
//Sends data to a text file students.txt 
void fillvector(vector <Student>& newMyClass) 

{ 
string name; 
float score1; 
float score2; 
float score3; 
float final; 
float tmp; 
cout << "Please enter the number of Students: " << endl; 
int classSize; 
cin >> classSize; 
for (int i = 0; i < classSize; i ++) 
    { 
    cout << "Enter Student's name" << endl; 
    cin >> name; 
    cout << "Enter Student's Score 1" << endl; 
    cin >> score1; 
    cout << "Enter Student's Score 2" << endl; 
    cin >> score2; 
    cout << "Enter Student's Score 3" << endl; 
    cin >> score3; 
    if(score1>score2) 
    { 
     float tmp = score1; 
     score1 = score2; 
     score2 = tmp; 
    } 
    if(score1>score3) 
    { 
    float tmp = score1; 
    score1=score3; 
    score3 = tmp; 
    } 
    if(score2>score3) 
    { 
    float tmp = score2; 
    score2=score3; 
    score3=tmp; 
    } 

    final = (score2+score3)/2; 

    Student newStudent (name, score1, score2, score3, final); 
    newMyClass.push_back(newStudent); 
    cout << endl; 

    ofstream myfile; 
    myfile.open ("students.txt", std::ofstream::out |std::ofstream::app); 
    myfile << name<< setw(5)<< score1<< setw(5)<<score2<<setw(5) <<score3<<setw(5)<<final<<setw(5)<<endl; 
    myfile.close(); 
    cout << "Copied to students.txt" << endl; 


    } 
    cout << endl; 
} 
//reads data from textfile students.txt 
//calculated teh minimum scores and maximum scores 
//sends the minimum and maximum scores to text file Results.txt 
void readToVector(vector <Student>& newMyClass) 

{ 
    string name; 
float score1; 
float score2; 
float score3; 
float finalScore; 
Student newStudent (name, score1, score2, score3, finalScore); 
unsigned int size = newMyClass.size(); 
ifstream fin("students.txt"); 
if (fin.is_open()) 
    {cout << "File open" << endl; 
    while(fin >> name >> score1 >> score2 >> score3 >> finalScore) 
     { 
    newStudent.setName(name); 
    newStudent.setScore1(score1); 
    newStudent.setScore2(score2); 
    newStudent.setScore3(score3); 
    newStudent.setFinal(finalScore); 
    newMyClass.push_back(newStudent); 
    //cout << newStudent.getName() << newStudent.getFinal() << endl; 

     } 
    fin.close(); 
    cout << endl; 
    Student studWMaxScore = newMyClass[0]; 
    float maxscore = studWMaxScore.getFinal(); 

for (unsigned int i =0; i < size; i++) 
{ 
    if (maxscore < newMyClass[i].getFinal()) 
    { 
    maxscore = newMyClass[i].getFinal(); 
    studWMaxScore = newMyClass[i]; 

    } 

} 
cout << "Maximum Score is " << maxscore << endl; 
    ofstream myfile; 
    myfile.open ("Result.txt", std::ofstream::out ); 
    myfile << "Maximum Score" << endl; 
    myfile << maxscore << endl; 
    myfile << "Name of the student with maximum score is " << endl; 
    myfile << studWMaxScore.getName() << endl << endl; 
    // myfile.close(); 
    cout << "Copied to Results.txt" << endl; 

Student stuWMinScore = newMyClass[0]; 
float minscore = stuWMinScore.getFinal(); 
for (unsigned int i =0; i < size; i++) 
{ 
    if (minscore > newMyClass[i].getFinal()) 
    { 
     minscore = newMyClass[i].getFinal(); 
     stuWMinScore = newMyClass[i]; 

    } 
} 
cout << "Minimum Score is " << minscore << endl; 
// ofstream myfile; 
// myfile.open ("Result.txt", std::ofstream::out); 
myfile << "Mimimum Score" << endl; 
myfile << minscore << endl; 
myfile << "Name of the student with minimum score is " << endl; 
myfile << stuWMinScore.getName() << endl << endl; 
// myfile.close(); 
cout << "Copied to Results.txt" << endl; 
    } 
else 
    cout << "file in not open" << '\n'; 

} 


//prints out the name and scores of each student 
void printVector (const vector<Student>& newMyClass) 

{ 
unsigned int size = newMyClass.size(); 
for (unsigned int i =0; i < size; i++) 

    { 
cout << "Student name is: "<< newMyClass[i].getName() << endl; 
cout << "Student Score 1 is "<< newMyClass[i].getScore1()<< endl; 
cout << "Student Score 2 is "<< newMyClass[i].getScore2()<< endl; 
cout << "Student Score 3 is "<< newMyClass[i].getScore3()<< endl; 
cout << "Student Final Score is " << newMyClass[i].getFinal() << endl; 
cout << endl; 
    } 

} 




int main() 

{ 
vector <Student> myClass; 
fillvector (myClass); 
readToVector(myClass); 
    printVector(myClass); 
} 
+0

您切斷了部分功能。最小值和最大值在哪裏計算? students.txt與程序位於同一目錄中嗎? – Stephen

+0

請提供更完整的代碼(包括學生課程)和students.txt文件的示例。 – steiner

+0

嗨,感謝您的回覆,我添加了類定義和最小最大值計算。 –

回答

0

錯誤的核心看起來是在這裏:

while(fin >> name >> score1 >> score2 >> score3 >> finalScore) 
{ 
    newStudent.setName(name); 
    newStudent.setScore1(score1); 
    newStudent.setScore2(score2); 
    newStudent.setScore3(score3); 
    newStudent.setFinal(finalScore); 
} 
fin.close(); 
newMyClass.push_back(newStudent); 

一旦壓痕是固定的,可以很容易地看到,newStudent僅被推到載體後,該文件已被完全讀取。 OP應該讓最後一名學生進入向量,但其他人都沒有。由於OP可能希望所有學生在矢量文件中的,

while(fin >> name >> score1 >> score2 >> score3 >> finalScore) 
{ 
    newStudent.setName(name); 
    newStudent.setScore1(score1); 
    newStudent.setScore2(score2); 
    newStudent.setScore3(score3); 
    newStudent.setFinal(finalScore); 
    newMyClass.push_back(newStudent); <- moved push_back to here 
} 
fin.close(); <- probably not necessary 

無法驗證代碼的其餘部分,因爲似乎沒有被 size --Crom的定義或僅分配知道這些循環是否覆蓋了正確的基礎 - 或者輸入的有效性,因爲沒有提供的輸入文件。如果解析規則錯誤,OP可能完全沒有讀取任何內容。調試器會比另一個堆棧溢出帖子更快地計算出來。 (OP已經解決了這個問題)

新的東西。

我不明白爲什麼輸入不解析。首先確保文件編碼爲ASCII文本。我不知道你必須在你的電腦上做什麼工具。我使用十六進制編輯器,可以讓您以字節值而不是字符查看文件,以查看這些字符是否與預期的ASCII值不匹配。文本編輯器可能會爲您轉換並隱藏其中的差異。

例如,UTF-8編碼的文件通常看起來與ASCII文件完全相同,只是在開始標記處有一點二進制文件,告知讀者程序它是一個UTF-8文件, ASCII。

如果不是,則存在問題。將文件重新保存爲ASCII文件。如果它是一個ASCII文件,那麼儘量減少時間。編寫一個小程序,除了打開文件外,什麼都不做,通過令牌讀取它的標記,並查看哪個標記無法解析。像這樣的:

int main() 
{ 
    ifstream fin("students.txt"); 
    if (fin.is_open()) 
    { 
     int line = 0; 

     while(true) // loop forever 
     { 
      line++; 
      if (!fin >> name) 
      { 
       std::cout << "Could not read name on line " << line << std::endl; 
       break; // exit loop 
      } 
      if (!fin >> score1) 
      { 
       std::cout << "Could not read score1 on line " << line << std::endl; 
       break; 
      } 
      if (!fin >> score2) 
      { 
       std::cout << "Could not read score2 on line " << line << std::endl; 
       break; 
      } 
      if (!fin >> score3) 
      { 
       std::cout << "Could not read score3 on line " << line << std::endl; 
       break; 
      } 
      if (!fin >> finalScore) 
      { 
       std::cout << "Could not read finalScore on line " << line << std::endl; 
       break; 
      } 
     } 
    } 
    else 
    { 
     std::cout << "Could not open file" << std::endl; 
    } 
} 

一旦你知道文件失敗的位置,你很有可能提出一個新的問題。這一個蔓延太多了。

+0

謝謝,我試着將push_back移到while循環中,並不影響結果。它仍然只讀取用戶對result.txt的輸入 –

+0

由於大小由no。的用戶輸入的學生,我已經嘗試使用newMyClass.size();以獲得大小。 –

+0

當你使用開發環境的調試器運行你的代碼時,你是否曾經進入while循環?或者說,去舊學校吧。在while循環中放置一個cout來打印學生的名字。輸入文件沒有名稱,沒有學生和解析規則是錯誤的。 – user4581301

相關問題