2016-04-27 4 views
-1

Ich habe den Code geändert, jetzt liest das Programm das Dateiformat students.txt korrekt, aber immer noch aus Benutzereingaben statt Studenten berechnet. txt
#include #include #include #include #include #include #include #includeLesen von einer Textdatei in einen Vektor von Objekten in einer Klasse schreiben in einer anderen TXT-Datei

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

Sie schneiden einen Teil der Funktion ab. Wo werden min und max berechnet? Befindet sich students.txt im selben Verzeichnis wie das Programm? – Stephen

+0

Bitte geben Sie einen vollständigeren Code (einschließlich der Student-Klasse) und ein Beispiel für die Datei students.txt an. – steiner

+0

Hallo, Danke für die Antwort, ich habe die Klassendefinitionen und die Min Max Berechnungen hinzugefügt. –

Antwort

0

Kern der Bug sieht hier zu sein:

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); 

Sobald die Vertiefung fixiert ist, ist es leicht zu sehen, dass newStudent nur in den Vektor nach geschoben wird die Datei vollständig gelesen wurde. OP sollte den letzten Schüler in den Vektor bringen, aber keinen der anderen. Da OP wahrscheinlich will alle Schüler in der Datei in dem Vektor,

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 

kann nicht den Rest des Codes überprüfen, weil es nicht nur eine Definition oder Zuweisung von size --Crom zu sein scheint weiß, ob diese for-Schleifen die richtige Masse abdecken - oder die Gültigkeit der Eingabe, weil es keine bereitgestellte Eingabedatei gibt. OP liest möglicherweise nichts, wenn die Parser-Regeln falsch sind. Der Debugger würde das viel schneller herausfinden als ein anderer Stack Overflow-Post. (OP hat seit dem angesprochen)

Neue Sachen.

Ich sehe nicht, warum die Eingabe auch nicht analysiert. Stellen Sie zuerst sicher, dass die Datei als ASCII-Text codiert ist. Ich weiß nicht, welche Werkzeuge du auf deinem Computer machen musst. Ich benutze einen Hex-Editor, lässt Sie die Datei in Byte-Werten statt Zeichen sehen, um zu sehen, ob die Zeichen nicht ihren erwarteten ASCII-Werten entsprechen. Ein Texteditor konvertiert möglicherweise für Sie und versteckt die Unterschiede. B. eine UTF-8-kodierte Datei wird oft genau so aussehen wie eine ASCII-Datei, mit Ausnahme eines binären Zeichens am Anfang, das dem Leseprogramm anzeigt, dass es sich um eine UTF-8-Datei handelt. ASCII.

Wenn nicht, gibt es das Problem. Speichern Sie die Datei erneut als ASCII-Datei. Wenn es sich um eine ASCII-Datei handelt, muss die Zeit minimiert werden. Schreiben Sie ein kleines Programm, das nichts anderes tut, als die Datei zu öffnen, es nach Token zu lesen und zu sehen, welches Token nicht analysiert werden kann. Etwas wie folgt aus:

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

Sobald Sie wissen, wo die Datei fehlschlägt, Sie sind in der großen Position eine neue Frage zu stellen. Dieser wälzt sich etwas zu sehr aus.

+0

Danke, ich habe versucht, die push_back in die While-Schleife zu verschieben, wirkt sich nicht auf das Ergebnis. es liest immer noch nur die Benutzereingabe in die result.txt –

+0

Da die Größe von der Nr. bestimmt wird. von Studenten, die vom Benutzer eingegeben wurden, habe ich versucht, newMyClass.size() zu verwenden; um die Größe zu bekommen. –

+0

Wenn Sie Ihren Code mit dem Debugger Ihrer Entwicklungsumgebung ausführen, geben Sie jemals die while-Schleife ein? Oder hack, geh zur alten Schule. Setzen Sie einen Cout in die While-Schleife, um den Namen des Schülers auszudrucken. Kein Name, kein Student und Ihre Parserregeln sind falsch für Ihre Eingabedatei. – user4581301

Verwandte Themen