2014-04-10 1 views
-2

실행할 때 outstandingunsatisfactory을 읽지 않습니다. 학년과 모든 것이 잘 작동하는 것 같습니다.어디서 잘못 됐습니까? 프로그램이 "미해결"및 "불만족"변수를 읽지 않습니다

a. 0에서 100까지의 범위의 시험 점수 모음을 읽는 프로그램을 작성하십시오. 프로그램은 각 점수의 범주를 표시해야합니다. 또한 미해결 점수 (90-100), 만족 점수 (60-89) 및 불만족 점수 (0-59)를 세어 표시해야합니다.

b. 실행이 끝날 때 평균 점수를 표시하도록 프로그램을 수정하십시오. 잘못가는 뭐죠

using namespace std; 

void displayGrade(int); 


int main() 
{ 
    const int SENTINEL = -1;  
    int score, sum = 0, count = 0, outstanding = 0, satisfactory = 0, unsatisfactory =0;         
    double average;    

    cout << "Enter scores one at a time as requested." << endl; 
    cout << "When done, enter " << SENTINEL << " to finish entering scores." << endl; 
    cout << "Enter the first score: "; 
    cin >> score; 
    while (score != SENTINEL) 
    { 
     sum += score; 
     count++; 
     displayGrade(score); 
     cout << endl<< "Enter the next score: "; 
     cin >> score; 
     if (score >= 90) 
      outstanding++; 
     else if (score >=60){ 
      satisfactory++; 
      if (score >= 0 && score <= 59) 
       unsatisfactory++; 
     } 
    } 

    cout << endl << endl; 
    cout << "Number of scores processed is " << count << endl; 
    cout << "Sum of exam scores is " << sum << endl; 
    cout << "The number of Outstanding scores is: " << outstanding << endl; 
    cout << "The number of Satisfactory scores is: " << satisfactory << endl; 
    cout << "The number of Unsatisfactory scores is: " << unsatisfactory << endl; 
    if (count > 0) 
    { 
     average = sum/count; 
     cout << "Average score is " << average << endl; 
    } 
    system("PAUSE"); 
    return 0; 
} 

void displayGrade(int score) 
{ 
    if (score >= 90) 
     cout << "Grade is A" << endl; 
    else if (score >= 80) 
     cout << "Grade is B" << endl; 
    else if (score >= 70) 
     cout << "Grade is C" << endl; 
    else if (score >= 60) 
     cout << "Grade is`enter code here` D" << endl; 
    else 
     cout << "Grade is F" << endl; 
} 

답변

3
else if (score >=60){ 
    satisfactory++; 

    if(score >= 0 && score <= 59) 
    unsatisfactory++; 
} 

당신이 볼 수 있을까요? 논리 점수 75 점을 다시 생각해보십시오. 벌금. 그러나 45는 어떨까요? 처음에는 else를 전달하지 않으므로 결코 불만족스럽게 도달하지 않습니다. ++ (안쪽 if 문까지 도달하지 못합니다). 점수가 안전하게 0 ~ 100 사이의 모든 시간으로 가정 할 수있는 경우

은 당신이 원하는 것은 (더

else if (score >=60){ 
    satisfactory++; 
} else if(score >= 0 && score <= 59) { 
    unsatisfactory++; 
} 

또는 짧은 같습니다 :.

else if (score >=60){ 
    satisfactory++; 
} else { 
    unsatisfactory++; 
} 

내가 뛰어난를 떠나

우리가 사물을 이해하는 데 도움이되기를 기쁘게 생각하지만, 우리는 학교 생활을 배우지 않으므로 결코 배울 수 없습니다. 버그를 찾는 좋은 방법은 디버거. while 루프에 들어가는 순간 멈추도록 IDE에 지시 할 수 있습니다. 그런 다음 줄을 따라갑니다. 모든 줄 멈춤 (line-halt)에서 어떤 줄을 볼 수 있고 (건너 뛴다.), 그리고 그 특정 시점에 변수 내용이 무엇인지 볼 수있다.

+0

답장을 보내 주시면 감사하겠습니다. 내 if 문은 while 루프에만있는 것으로 생각합니다. 그래서 나는 주먹 번호를 입력 할 때마다 그것이 중요하지 않습니다. 'if (score <0 || score> 100)' 'cout << "잘못된 번호 0과 100 사이의 숫자를 입력하십시오. \ n" – user3361763

관련 문제