나는 사용자 입력 (마크)을 취하고 이러한 마크의 평균을 문자 등급으로 출력하는 프로그램을 만들려고합니다. 나는 프로그램을 성공적으로 평균을 계산하는 데 성공했다고 믿지만 올바른 문자 등급을 출력하지 않을 것입니다.do-while 루프 프로그램이 필요하십니까? (C++)
제안 사항? 편집 : 내 출력은 항상 "F"등급으로 비율을 제공하기 때문에 아마도 그것은 계산에 뭔가가 ..., 사물의
// Used iomanip to display averages for possible three digit or greater answers.
#include <iostream>
#include <iomanip>
using namespace std;
// Set up my program using only main()
int main()
{
//Created the variable 'mark' so the user input can be stored and tested in the do-while condition.
//Variable 'sum' and 'average' to calculate mark and then be tested against letter grades.
//'counter' used to keep track of number of terms being averaged.
double mark=0, sum=0, average=0;
int counter=-1;
// Do-while statement to test the loop. Gets input from user, and tests whether it is a a valid letter grade.
// Marks below 0 or above 100 will test true and loop back re-promting the user for another mark.
// If tested condition is false, then if statements then test the mark and output the letter grade.
cout << "Please enter marks to calculate average, and enter -1 to stop and find letter equivalence. " << endl;
do
{
if (mark < 0 || mark > 100)
{
cout << "Entered mark is invalid. Please try again, or enter -1 to stop and find letter equivalence. " << endl;
break;
}
sum = sum + mark;
counter++;
cin >> mark;
}
while (mark != -1);
average = sum/counter ;
//What happens when above statement is false...
if (mark >= 90)
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ ";
}
else if (mark >=80)
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A ";
}
else if (mark >=70)
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of B ";
}
else if (mark >=60)
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of C ";
}
else if (mark >=50)
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of D ";
}
else
{
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of F ";
}
return 0;
}
무엇이 문제입니까? – jman