Using C Write a program that will define a structure consist
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.
Solution
// C++ code student passed or fail
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
using namespace std;
// structure
struct student
{
string name;
int ID;
short marks1,marks2,marks3;
float average;
char letterGrade;
};
// function to read the name, id number and three test scores, the function will return a structure.
student setter()
{
student e;
cout<<\"Enter your name : \";
std::getline(std::cin,e.name);
cout<<\"Enter your ID : \";
cin>>e.ID;
cout<<\"Enter 3 test scores : \ \";
cin>>e.marks1>>e.marks2>>e.marks3;
return e;
}
// 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
void avg(student &e)
{
e.average=(e.marks1+e.marks2+e.marks3)/3;
}
// function that takes the average as argument and returns the grade (‘A’,’B’,’C’,’D’, or ’F’) using standard grading.
char grade(float average)
{
if(average>=90)
return \'A\';
else if(average>=80)
return \'B\';
else if(average>=70)
return \'C\';
else if(average>=60)
return \'D\';
else
return \'F\';
}
// function that prints the message “You Passed” or “You Failed”.
void message(float average)
{
if(average>=60)
cout<<\" You passed\ \";
else
cout<<\" You failed\ \";
}
// function that prints each data member of the structure on a separate line
void print(student e)
{
cout<<\"\ Name : \";
cout<<e.name;
cout<<\"\ ID : \"<<e.ID;
cout<<\"\ Tests : \";
cout<<\"\ 1-\"<<e.marks1;
cout<<\"\ 2-\"<<e.marks2;
cout<<\"\ 3-\"<<e.marks3;
cout<<\"\ Average : \"<< e.average;
message(e.average);
}
int main()
{
student emp;
emp = setter();
avg(emp);
emp.letterGrade = grade(emp.average);
print(emp);
return 0;
}
/*
output:
Enter your name : ayush verma
Enter your ID : 453
Enter 3 test scores :
78
88
98
Name : ayush verma
ID : 453
Tests :
1-78
2-88
3-98
Average : 88 You passed
*/


