In: Computer Science
Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display the student's name, and a letter grade such as A, B, C, D, or F. It shall have a Student class with necessary private data members and constructor and public methods, such as getName, getGrade, calcScore, convertToGrade, etc. Try to create a Student object in main(), then invoke its methods to perform the tasks.
#include <iostream>
using namespace std;
#define MAX 10
class student
{
private:
char name[30];
int total=0;
float avg,score[10];
char grade;
public:
void getName(void);
void getGrade(void);
void calcScore(void);
void convertToGrade(void);
};
//member function definition, outside of the class
void student::getName(void){
cout << "Enter name: " ;
cin >> name;}
void student::calcScore(void){
cout << "Enter total marks out of 100: ";
for(int i=0;i<10;i++)
cin >> score[i];
for(int i=0;i<10;i++)
total=total+score[i];
avg=(float)total/100*100;
}
void student::convertToGrade(void)
{
if(avg<=)
grade='F';
else if((avg>)&&(avg<=))
grade='E';
else if((avg>)&&(avg<=))
grade='D';
else if((avg>)&&(avg<=))
grade='C';
else if((avg>)&&(avg<=))
grade='B';
else if((avg>))
grade='A';
}
void student::getGrade(void){
cout << "Student details:\n";
cout << name <<" "<<grade;
}
int main()
{
student std[MAX];
int j;
for(j=0;j< 10; j++){
std[j].getName();
std[j].calcScore();
std[j].convertToGrade();
}
cout << endl;
for(j=0;j< 10; j++){
cout << "Details of student " << (j+1) << ":\n";
std[j].getGrade();
}
return 0;
}
-> In the case where grade is alloted in the converttograde function
i have left the limits for you to have them as you desire . As you have not mentioned for what range what grades should be alloted. So you can have them edited as you want. Otherwise everything is as desired.
#you can ask if any doubt persists..