Using C++: Write a program that will define a structure consisting of the follow
ID: 3921322 • Letter: U
Question
Using C++:
Write a program that will define a structure consisting of the following data members, “fields”:
Name - a string
Student id number – an integer.
Three test scores – positive short integers.
Average –a float
Grade – a character.
Input to the program consists of:
name
id number
three test scores
Requirements:
Use a function to read the name, id number and three test scores, the function will return a structure.
A function to find the average, the function is a void function that takes the entire structure as an argument (by reference) and calculates the average.
A function that takes the average as argument and returns the grade (‘A’,’B’,’C’,’D’, or ’F’) using standard grading.
A function that prints each data member of the structure on a separate line and calls another function that prints the message “You Passed” or “You Failed”.
Display should be like this:
Employee Name : Roberth Schultz
Id Number : 2345
Tests :
1-78
2-88
3-98
Average : 88.00
Grade : B You Passed.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct studentDetails {
string name;
int studentID;
int score[3];
float average;
char grade;
};
studentDetails input()
{
studentDetails student;
cout<<"Enter the student name: ";
cin>>student.name;
cout<<" Enter student ID: ";
cin>>student.studentID;
cout<<" Enter the three test scores";
for(int i=0;i<3;i++)
{
cout<<" Test "<<i+1<<" mark: ";
cin>>student.score[i];
}
return student;
}
void calculateAverage(studentDetails *data)
{
int sum=0;
for(int i=0;i<3;i++)
{
sum+=data->score[i];
}
data->average=sum/3;
cout<<"Average of marks scored in 3 subjects is :";
cout<<data->average;
}
char calculateGrade(float avg)
{
if(avg>=90)
{
return 'A';
}
else if(avg>=80 && avg<90)
{
return 'B';
}
else if(avg>=70 && avg<80)
{
return 'C';
}
else if(avg>=60 && avg<70)
{
return 'D';
}
else
{
return 'E';
}
}
void display()
{
studentDetails sd;
cout<<" Employee Name: "<<sd.name;
cout<<" Id Number: "<<sd.studentID;
cout<<" Tests: ";
for(int j=0;j<3;j++)
{
cout<<j+1<<"-"<<sd.score[j];
cout<<" ";
}
cout<<"Average: "<<sd.avg;
cout<<" Grade: "<<sd.grade<<getMessage(sd.grade);
}
String getMessage(grade)
{
if(grade='A' || grade='B' || grade='C')
{
return "You Passed.";
}
else
{
return "You Failed.";
}
}
int main()
{
input();
studentDetails data;
calculateAverage(&data);
calculateGrade(data.average);
display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.