2014-11-08 2 views
-1

수업 중에 아이스 스케이트 프로그램을 만들고 있는데 런타임 중에 오류 메시지가 표시됩니다. 컴파일러가 Judge 함수에서 성능 및 기술 점수를 추가하려고 할 때 발생합니다. 도움이 필요하시면 언제든지 알려 주시기 바랍니다.내 코드에서 첫 번째 예외가 발생했습니다.

편집 : 오류 메시지는 "가 0xc0000005 :. 액세스 위반 위치 0xCDCDCDD5를 읽고 최초의 기회 IceSkating1.exe에서 0x00FE4686에서 예외"입니다 및 "0x00FE4686 IceSkating1.exe에서 처리되지 않은 예외 : 0xC0000005 : 액세스 위반 읽기 위치 0xCDCDCDD5."

#include <iostream> 
#include <iomanip> 
using namespace std; 

typedef int *intptr; 

class Judge 
{ 
private: 
    int performance, technical, total = 0; 

public: 
    void SetPerformance(int a) { performance = a; } 
    void SetTechnical(int a) { technical = a; } 
    void SetTotal() { total = performance + technical;} //EXCEPTION OCCURS HERE 
    int GetTotal() { return total; } 
}; 

class Skater 
{ 
private: 
    int ID; 
    Judge* judges; 
    int *ranks; 

public: 
    void SetID() 
    { 
     int t; 
     cout << "Please enter skater ID: "; 
     cin >> t; 
     ID = t; 
    } 

    int GetID() 
    { 
     return ID; 
    } 

    void Judges(int qnty) 
    { 
     int p, t; 
     judges = new Judge[qnty]; 


     for (int count = 0; count < qnty; count++) 
     { 
      cout << "Please enter the peformance score from judge " << count + 1 << ":"; 
      cin >> p; 
      while ((p > 6) || (p < 0)) 
     { 
      cout << "Invalid score. Please enter a value between 0 - 6. \n"; 
      cin >> p; 
     } 

     judges[qnty].SetPerformance(p); 


     cout << "Please enter the technical score from judge " << count + 1 << ":"; 
     cin >> t; 

     while ((t > 6) || (t < 0)) 
     { 
      cout << "Invalid score. Please enter a value between 0 - 6. \n"; 
      cin >> t; 
     } 

     judges[qnty].SetTechnical(t); 

    } 
} 

int GetJudgeTotal(int judgeqnty) 
{ 
    return judges[judgeqnty].GetTotal(); 
} 

void SetRank(int judgeqnty, int rank) 
{ 
    ranks = new int[judgeqnty]; 
    rank = ranks[judgeqnty]; 
} 

int GetRank(int judgeqnty) 
{ 
    return ranks[judgeqnty]; 
} 
}; 


int main() 
{ 
Skater* s; 
int skaterqnty, judgeqnty; 

cout << "Please enter number of contestants: "; 
cin >> skaterqnty; 

while ((skaterqnty > 24) || (skaterqnty < 0)) 
{ 
    cout << "Please enter a valid amount. \n"; 
    cin >> skaterqnty; 
} 

s = new Skater[skaterqnty]; 

for (int count = 0; count < skaterqnty; count++) { 
    s[count].SetID(); 
} 


cout << "Please enter odd number of judges: "; 
cin >> judgeqnty; 
if ((judgeqnty % 2) == 0 || (judgeqnty > 11)) 
{ 
    cout << "Please enter valid number of judges. \n"; 
    cin >> judgeqnty; 
} 

for (int count = 0; count < skaterqnty; count++) 
{ 
    cout << "Please enter scores for skater " << s[count].GetID() << ": \n"; 
    s[skaterqnty].Judges(judgeqnty); 
} 

cout << s[0].GetJudgeTotal(0); 

/*int jTotals[judgeqnty][skaterqnty]; 

for (int column = 0; column < skaterqnty; column++){ 
for (int row = 0; row < judgeqnty; row++){ 
jTotals[row][column] = s[column].JudgeTotal(judgeqnty); 
} 
}*/ 

cout << "---Beginning rank sort---\n"; 
for (int m = 0; m < judgeqnty; m++) { 
int _rank = 1; cout << "---First level of loop\n"; 
for (int n = 0; n < skaterqnty; n++) { cout << "---Second level \n"; 
for (int p = 0; p < skaterqnty; p++) { cout << "---Third level \n"; 
if (s[n].GetJudgeTotal(m) < s[p].GetJudgeTotal(m)) 
{ 
_rank++; 
cout << "Control statement completed\n"; 
} 
} cout << "End third level\n"; 
s[n].SetRank(m, _rank); cout << "End second level\n"; 
} cout << "First level completed. \n"; 
} 

for (int a = 0; a < skaterqnty; a++){ 
    for (int b = 0; b < judgeqnty; b++) 
    { 
     cout << "Current Ranking:\nSkater " << s[a].GetID() << ":\t" << s[a].GetRank(b) << "\t"; 
    } 
} 

cout << "\nDONE."; 

} 
+0

을 그리고 예외입니다 :

judges[qnty].SetPerformance(p); 

하지만 루프에서이 일을하고, 그래서 아마 당신은 의미 : 당신은 뭐하는거야? –

+0

예외가 바로 발생합니까? 해당 코드를 디버깅 했습니까? 그렇다면 크래시가 발생하기 전에 어떤 변수가 변수 값을 가지고 있는지 알고 있습니까? – Alfabravo

답변

3

당신은 크기 qnty 배열 할당 :

judges = new Judge[qnty]; 

을 다음 qnty 번째 요소에 액세스하려고 : 이것은 undefined behaviour (요소가로 번호가 매겨져있다

judges[qnty].SetPerformance(p); 
    judges[qnty].SetTechnical(t); 

0 ~ qnty-1).

judges[count]이라고 쓰시겠습니까?

+0

나는 고마워하지 않았다! 그러나이를 변경하고 다시 실행하면 동일한 문제점이 발생합니다. | – TacoCats

0

잘못된 판사의 점수가 업데이트되었습니다. ....

judges[count].SetPerformance(p); 
관련 문제