2016-06-26 9 views
1

私は、学生と学年のオブジェクトを入力して並べ替え、平均GPAを計算してファイルに出力するコードを書いています。私の現在の問題は、私の生徒オブジェクトの出力は、並べ替えて印刷した後に出力されるということですが、コードを実行するとループが終了しないようです。私はofstreamを渡しているプリント機能を持っています。デバッグから、私はout ofstreamオブジェクトがすべてを読み込むことを知っていますが、その後はフリーズして他に何も出力しないようです。私が参照によって間違って渡されたかどうか、あるいは何か他のことをする必要がある場合は誰かに教えてもらえますか?コンパイラが実行しなかったエラーは、コードに記録されています。ストリームオブジェクトを正しく渡すにはどうすればいいですか?

申し訳ありませんが、より多くの情報のためので、以下に一度だけ表示され、「それを通じて得た」どのよう

output code in text file

実行されているもので、それをチョキお知らせです。ファイルを開いて出力するのは問題ではないことが分かっています。なぜなら、実際には適切なものが出力されたからです。また、修正のために、コンパイラは実際にすべてのコンパイルを行います。ターミナルが停止し、何も出力されないのは実行時です。印刷機能付き

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

} 

生徒のクラス

学生のcppファイルを正しくofstreamのオブジェクトを渡す方法に関するご質問は、あなたがそれを正しく渡している答えで

#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

[最小を投稿してください:その他の問題が、calcgrades()関数は、このif文で、whileループの後、ループの反復の最後に、アクセス違反がありますがあるかもしれません、完了、検証可能な例](http://stackoverflow.com/help/mcve) –

+1

コンパイラエラー –

答えて

0

なぜそれが機能しませんか?あなたのコードから観察できるものから、うまくいくはずです。私はこれらのコード行で出力ファイル名をデバッグすることをお勧め:

ofstream outtxt; 

string out_file = argv[4]; 

outtxt.open(out_file); 

はオープ​​ン関数はopen関数を呼び出した後にフラグを失敗:: IOSをチェックして成功したことを確認します。

は、開封後はこのような何かを試してみてください:

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

私はこのことができます願っています。

編集:

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

を追加してください。ファイルを開いて実際に正しいものを出力したので問題がないことを知っています。上記の追加の図を参照してください^ –

+0

@RickGiovanini "students [i] .print(outtxt);をステップ実行した後、デバッガは2番目のループに何を表示しますか? –

+0

申し訳ありませんが、これは話題にはならないかもしれませんが、実際にはデバッガはありません。私はクラスのための端末と一緒にubuntuと崇高なテキストを使用する必要があり、私はどのように視覚スタジオ(メインなどのargvとchar)と簡単にそれを整列するのかわかりません。それはおそらく私の最大の問題の一つです。何か案は? –

関連する問題