2016-06-26 10 views
1

Ich schreibe Code, um Schüler und Klassenobjekte einzugeben, sie zu sortieren, ein durchschnittliches gpa zu berechnen und sie in eine Datei auszugeben. Mein aktuelles Problem ist, dass die Ausgabe meiner Schülerobjekte gegeben ist, nachdem ich sie sortiert und ausgedruckt habe, aber der Code scheint die Schleife nicht zu verlassen, wenn ich das tue. Ich habe eine Druckfunktion, in der ich einen Ofstream passiere. Aus dem Debuggen weiß ich, dass das Out-of-Stream-Objekt alles liest, aber dann scheint es einzufrieren und nichts anderes auszugeben. Kann mir jemand sagen, ob ich falsch durch einen Verweis gegangen bin oder ob ich etwas anderes machen muss? Der Fehler, bei dem der Compiler nicht weiter durchgegangen ist, ist im Code markiert.Wie gibt man ein Objekt aus?

Sorry, so für weitere Informationen, unter einem es von schnippeln ist, was

output code in text file

Hinweis ausgeführt wird, wie „bekam durch sie“ wird nur einmal angezeigt. Ich weiß, dass es kein Problem ist, die Datei zu öffnen und auszugeben, weil sie tatsächlich das richtige Material ausgegeben hat. Zur Korrektur kompiliert der Compiler sogar alles in Ordnung. Es ist zur Laufzeit, wo das Terminal aussetzt und nichts mehr ausgegeben wird.

#include <vector> 
#include <algorithm> 
#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include "Grade.h" 
#include "Student.h" 

using namespace std; 

const double A_NUM = 4.0, B_NUM = 3.0, C_NUM = 2.0, D_NUM = 1.0, E_NUM = 0.0, B_PLUSSNUM = 3.4, C_PLUSSNUM = 2.4, D_PLUSSNUM = 1.4; 
const double A_MINUSNUM = 3.7, B_MINUSNUM = 2.7, C_MINUSNUM = 1.7, D_MINUSNUM = 0.7; 
const int FIRST_INDEX= 0; 
const int START_NUM = 0; 
const int LESS_ONE = 1, ABOVE_ONE = 1; 
const int START_GRADE = 0; 

void calcgrades(vector<Grade>& grades)//function that calculates gpa based on letter grades 
{ 
    const string A = "A", A_MINUS = "A-", B_PLUSS = "B+", B = "B", B_MINUS = "B-", C_PLUSS = "C+", C = "C", C_MINUS = "C-", D_PLUSS = "D+", D = "D", D_MINUS = "D-", E = "E"; 
    int counter = START_NUM;//used to keep track of current student and current total grade 
    double current_grade = START_NUM; 

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

     //while loop to get the student's current total grade if the next student id is different than the first one. 
     while(i < grades.size()-LESS_ONE && grades[i].getid() == grades[i+ABOVE_ONE].getid()) 
     { 
      if (grades[i].getgrade() == A) 
      { 
       current_grade == A_NUM + current_grade; 
      } 
      if (grades[i].getgrade() == B) 
      { 
       current_grade == B_NUM + current_grade; 
      } 
      if (grades[i].getgrade() == C) 
      { 
       current_grade == C_NUM + current_grade; 
      } 
      if (grades[i].getgrade() == D) 
      { 
       current_grade == D_NUM + current_grade; 
      } 
      if (grades[i].getgrade() == E) 
      { 
       current_grade == E_NUM + current_grade; 
      } 
      if (grades[i].getgrade() == B_PLUSS) 
      { 
       current_grade == B_PLUSSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == C_PLUSS) 
      { 
       current_grade == C_PLUSSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == D_PLUSS) 
      { 
       current_grade == D_PLUSSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == A_MINUS) 
      { 
       current_grade == A_MINUSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == B_MINUS) 
      { 
       current_grade = B_MINUSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == C_MINUS) 
      { 
       current_grade = C_MINUSNUM + current_grade; 
      } 
      if (grades[i].getgrade() == D_MINUS) 
      { 
       current_grade = D_MINUSNUM + current_grade; 
      } 

     } 


     if (grades[i+ABOVE_ONE].getid() != grades[i].getid() || i == grades.size()-LESS_ONE) 
     { 

      //computes the average if the currentid is not equal to the nextid 
      double avggpa = current_grade/counter; 

      grades[i].newgpa(avggpa); 
      counter = START_NUM;//resets counter for a new student to get his gpa 
     } 

     counter++; 
    } 
} 



int main(int argc, char* argv[]) 
{ 
    string student_file, grades_file, idnum, name, address, pnum, course, gletter; 

    //creates student and grade objects from respective classes 
    vector<Student> students; 
    vector<Grade> grades; 


    student_file = argv[1]; 

    //reads in information from file to import into student objects 
    ifstream sfile; 

    sfile.open(student_file); 




    while(getline(sfile, idnum))//gets the student information from student file 
    { 
     getline(sfile, name); 
     getline(sfile, address); 
     getline(sfile, pnum); 

     //creates student objects to import info. into, and inserts into students vector 
     Student s(idnum, name, address, pnum); 
     students.push_back(s); 

    } 

    sfile.close(); 

    //opens information from the grade file 
    grades_file = argv[2]; 

    //reads from file to import into grades objects 
    ifstream gfile; 

    gfile.open(grades_file); 

    //gets the grade information from the grades file 
    while(getline(gfile, course)) 
    { 
     getline(gfile, idnum); 
     getline(gfile, gletter); 

     //creates grade objects to import info, and inserts into grades vector 
     Grade g(course, idnum, gletter, START_GRADE); 
     grades.push_back(g); 
    } 

    gfile.close(); 

    //reads the query file 
    string query_file; 
    query_file = argv[3]; 

    ifstream qfile; 

    qfile.open(query_file); 

    //reads from query file 
    //creates vector to store each student number 
    vector<string> query_nums; 
    string incheck; 

    while(getline(qfile, incheck)) 
    { 
     query_nums.push_back(incheck); 
    } 

    qfile.close(); 

    //sorts the information for the students 
    sort(students.begin(), students.end()); 


    //sorts the information for the grades 
    sort(grades.begin(), grades.end()); 

    ofstream outtxt; 

    string out_file = argv[4]; 

    outtxt.open(out_file); 

    //outputs the student information, sorted now. 
    for(int i = 0; i < students.size();i++) 
    { 
     students[i].print(outtxt); 

     outtxt << "got through it"; 
    } 

    //compiler did not get past here! 
outtxt << "We're here!"; 

    //outputs the grades with student id's, now sorted 
    for (int i = 0; i < grades.size(); i++) 
    { 
     grades[i].print(outtxt); 
     outtxt << "\n\n" << "so gay!"; 
    } 


    //calculates the average gpa for every student 
    calcgrades(grades); 



    for(int i = 0; i < query_nums.size(); i++)//goes through each query number to check first 
    { 
     for (int j = 0; j < students.size(); j++)//after, goes through each student object 
     { 
      if (query_nums[i] == students[j].getid()) 
      { 
       //finds the gpa in the grades class that matches the student id 
       for (int k = 0; k < grades.size(); k++) 
       { 
        if (grades[k].getid() == query_nums[i])// 
        { 
         //outputs the resulting id, avg gpa, and name of matching students 
         outtxt << grades[i].getid() << "\tthere is nothinghere" << grades[i].getgpa() << "\t" << students[i].getname(); 

        } 
       } 

      } 
     } 
    } 

    outtxt.close(); 

return 0; 

} 

Schüler-Klasse mit Druckfunktion

Schüler CPP-Datei

#include "Student.h" 
#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 

using namespace std; 

//Student constructor, used to make student objects 
    Student::Student(string id,string nm,string add,string pnumber) 
{ 
    idnum = id; 
    name = nm; 
    address = add; 
    pnum = pnumber; 
} 

    void Student::print(ofstream& out) 
    { 
     out << name << endl << idnum << endl << pnum << endl << address << endl; 
    } 

    string Student::getname() 
    { 
     //returns the student name 
     return name; 
    } 


    string Student::getid() 
    { 
     return idnum; 
    } 
+2

Bitte senden Sie uns eine [Minimal , Komplettes, überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) –

+1

Bitte fügen Sie den Compiler-Fehler –

Antwort

0

In Antwort Ihre Frage, wie richtig ein ofstream Objekt zu übergeben, können Sie es richtig sind vorbei.

Warum funktioniert es nicht? Von dem, was von Ihrem Code aus beobachtet werden kann, sollte es gut funktionieren. Ich würde vorschlagen, die Ausgabedateinamen in diesen Zeilen Code debuggen:

ofstream outtxt; 

string out_file = argv[4]; 

outtxt.open(out_file); 

Stellen Sie sicher, dass die offene Funktion, indem Sie die ios gelang :: Flag fehlschlagen, nachdem die offene Funktion aufrufen.

Probieren Sie etwas wie dies nach dem Öffnen:

if (ios::fail) 
    cout << "fail"; 

Ich hoffe, das hilft.

Edited: Es können auch andere Probleme, aber die calcgrades() Funktion in dieser if-Anweisung für Schleifeniterationslatenzzeit nach der while-Schleife, eine Zugriffsverletzung auf der letzten hat:

if (grades[i+ABOVE_ONE].getid() != grades[i].getid() || i == grades.size()-LESS_ONE) 
+0

hinzu Ich weiß, dass es kein Problem ist, die Datei zu öffnen und auszugeben, weil sie tatsächlich das richtige Zeug ausgegeben hat. Siehe zusätzliches Bild oben^ –

+0

@RickGiovanini Was zeigt der Debugger in der zweiten Schleife, nachdem er über "students [i] .print (uttxt)" gestoßen ist? –

+0

Sorry, ich weiß, dass das Thema nicht stimmt, aber ich habe keinen Debugger. Ich muss ubuntu und sublime Text zusammen mit dem Terminal für eine Klasse verwenden, und ich weiß nicht, wie man das leicht mit Visual Studio (argv und char im Haupt, usw.) ausrichten kann. Also, das ist wahrscheinlich eines meiner größten Probleme. Irgendwelche Ideen? –

Verwandte Themen