2014-06-15 67 views
-4

我有一個C++程序,我一直在努力,它顯示順序訪問文件的學生成績,然後使用相同的文件來計算和顯示每個學生gpa。在調用displayGpa()後,它會顯示「運行時檢查失敗#2 - 變量'gpa'周圍的堆棧已損壞。」運行時檢查失敗#2 - 圍繞變量'gpa'堆棧已損壞

這裏是.cpp文件給我的麻煩:

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

using namespace std; 

void sRecords::displayGpa() 
{ 

    string firstName, lastName; 
    string grade[4]; 
    double gpa[4]; 
    double finalGpa[4]; 
    ifstream inputFile; 
    inputFile.open("studentRecords.txt"); 

    while (inputFile) 
    { 
     inputFile >> firstName >> lastName; 
     int x = 0; 
     while (x <=3) 
     { 
      inputFile >> grade[x]; 
      if (grade[x] == "A") 
      { 
       gpa[x] = 4.00; 
      } 
      else if (grade[x] == "A-") 
      { 
       gpa[x] = 3.70; 
      } 
      else if (grade[x] == "B+") 
      { 
       gpa[x] = 3.33; 
      } 
      else if (grade[x] == "B") 
      { 
       gpa[x] = 3.00; 
      } 
      else if (grade[x] == "B-") 
      { 
       gpa[x] = 2.70; 
      } 
      else if (grade[x] == "C+") 
      { 
       gpa[x] = 2.30; 
      } 
      else if (grade[x] == "C") 
      { 
       gpa[x] = 2.00; 
      } 
      else if (grade[x] == "C-") 
      { 
       gpa[x] = 1.70; 
      } 
      else if (grade[x] == "D+") 
      { 
       gpa[x] = 1.30; 
      } 
      else if (grade[x] == "D") 
      { 
       gpa[x] = 1.00; 
      } 
      else if (grade[x] == "D-") 
      { 
       gpa[x] = .70; 
      } 
      else 
      { 
       gpa[x] = 0.00; 
      } 
     x++; 
     } 
     finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3])/4; 
     cout << firstName << " " << lastName << " " << finalGpa[x] << endl; 
    } 
    inputFile.close(); 
} 
void sRecords::displayHeader() 
{ 
    cout << "\t-------------------------------" << endl; 
    cout << "\t\tSTUDENT RECORDS" << endl; 
    cout << "\t-------------------------------" << endl << endl; 
} 

void sRecords::printRecords() 
{ 
    ifstream inputFile; 
    inputFile.open("studentRecords.txt"); 

    string string1, string2, string3, string4, string5, string6; 
    cout << setw(12) << left << "FIRST NAME" << setw(11) << left << "LAST NAME"; 
    cout << setw(9) << left << "ENGLISH" << left << setw(6) << "MATH"; 
    cout << setw(9) << left << "SCIENCE" << left << setw(12) << "CHEMISTRY" << endl << endl; 

    while (inputFile) 
    { 
     inputFile >> string1 >> string2 >> string3 >> string4 >> string5 >> string6; 

     cout << setw(12) << left << string1 << setw (11) << left << string2; 
     cout << setw(9) << left << string3 << setw(6) << left << string4; 
     cout << setw(9) << left << string5 << setw(12) << left << string6 << endl; 
    } 
    cout << endl; 
    inputFile.close(); 
} 
+0

TL; DR;聽起來很像你擊中了未定義的行爲! –

+0

'while(inputFile)'看起來像一個無限循環,我不知道它是如何工作的第一次。如果你想閱讀整個文件,你應該做一些像'while(!inputFile.eof())' – Gufino2

+0

@ Gufino2 http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-條件考慮錯誤 –

回答

0

你有一個無限循環 - 你好像有迷惑你的內循環和外循環的邏輯。

它看起來像你需要改變:

  else 
      { 
       gpa[x] = 0.00; 
      } // end of if/else 
     } // end of inner while loop 
     finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3])/4; 
     cout << firstName << " " << lastName << " " << finalGpa[x] << endl; 
     x++; 
    } // end of outer while loop 

到:

  else 
      { 
       gpa[x] = 0.00; 
      } // end of if/else 
      finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3])/4; 
      cout << firstName << " " << lastName << " " << finalGpa[x] << endl; 
      x++; 
     } // end of inner while loop 
    } // end of outer while loop 
相關問題