2014-10-05 5 views
-4

내가 정말로해야 할 일은 성적을 표시하는 것입니다. 그러나 어떤 이유인지 나는 두뇌 방구를 가지고 있고, 인생에 대해 알아낼 수는 없습니다. 당신이 알고 있어야하는 사람들을 위해, 전체 과제는 내가 숫자가있는 텍스트 파일을 받았다는 것입니다. 각 행은 한 학생에게 해당하는 네 가지 시험 점수로 구성됩니다. 각 학생의 평균을 계산하고 성적을 표시해야합니다. 내가 설정 한 코드로 어떻게 진행할 수 있습니까? 여기 점수를 어떻게 표시 할 수 있습니까?

는 텍스트 파일에 나타나는 숫자 :

다음
44 55 77 88 
79 88 100 99 
77 99 98 99 
100 88 89 100 
55 56 40 77 
100 100 99 95 
88 84 87 88 
96 97 99 100 
30 44 77 55 
79 77 88 0 
54 52 60 77 
88 77 88 77 
44 77 10 95 

당신이 단지 각 학생의 등급을 표시해야 할 것으로 보인다 코드

// This program calculates a student's average test score, displays 
// their grade and then reads the file back to the user. 
#include <iostream> 
#include <fstream> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
    ifstream testscores; // Creating an object to output file. 
    testscores.open("grades.txt"); // Opening the file. 

    char grade; // A student's grade. 
    int score1, score2, score3, score4; // Four test scores. 
    double average; // The average test score for each student. 

    cout << "These are the scores and grades for each student.\n\n"; 
    cout << "Averages   Grade"; 
    cout <<"\n--------   -----\n"; 

    if (!testscores) // Checking for errors otherwise. 
     cerr << "Error opening file.\n"; 
    else 
    { 
     /*A loop that reads each number until it reaches the end of the file */ 
     while(testscores >> score1 >> score2 >> score3 >> score4) 
     { 
      // Calculate the average. 
      average = (score1 + score2 +score3 +score4)/4.0; 
      cout << setw(2) << average << endl; // Display the average. 
     } 
     testscores.close(); // closing the file. 
    } 
    return 0; 
} 
+2

당신이 묻는 것을 반영하도록 제목을 업데이트하십시오. "약간의 지침이 필요합니다"는 엄청나게 구체적이지 않습니다. –

+0

나는 아직도 당신의 문제가 무엇인지 알아 내려고합니다. 평균을 계산했는데 이제 그 평균을 기반으로 등급을 계산하고 싶습니까? – dyp

+0

예, while 루프 내에서 if/else 문을 사용할 수 있다고 생각합니다.하지만 그건 내 최선의 추측입니다. – Deathslice

답변

0

입니다. 이를 위해서는 각 학생의 average을 표시 한 직후에 if...else을 사용할 수 있습니다.

if(average>=90) 
grade='A'; 
if(average<90 && average >=80) 
grade='B'; 
if(average<80 && average >=70) 
grade='C'; 
// and so on...then print: 
cout<<"Grade="<<grade<<endl; 
관련 문제