#include <iostream>
using namespace std;
int studentNumber = 1; //if we want to add one more functioncalling
//so number will
be increment
//define the prototypes
char GetGrade(double mark); //this function gets letterequivalent of numeric mark
void ShowResult(int id, double firstMark, doublesecondMark,
double finalMark);
bool IsValidMark(double mark); //here is if block
//one more if block is in ShowResult()
//I don't know do mark can be negative, so I've addedboolean function, that checks marks
// if it can be negative you can delete some if blocks(imarked it)
int main()
{
int studentId;
double firstMark;
double secondMark;
double finalMark;
cout << "Enter the srudentID: ";
cin >> studentId;
cout << "Enter the firstmark: ";
cin >> firstMark;
cout << "Enter the secondmark: ";
cin >> secondMark;
cout << "Enter the finalmark: ";
cin >> finalMark;
cout << "Theresults:\n";
//Headers
cout << "Student_Number"<< "\t" << "Student_Id" << "\t"
<< "First" << "\t" <<
"Second" << "\t" <<"Final" << "\t" << "Total" <<
"\t" << "Grade" << endl;
ShowResult(studentId, firstMark,secondMark, finalMark);
return 0;
}
void ShowResult(int id, double firstMark, double secondMark,double finalMark)
{
if (!(IsValidMark(firstMark))) //I'veadded one more brackets to make this line more clearly, to avoid mistakes
{
cout << "Thefirst mark is invalid!" << endl;
return;
}
if (!(IsValidMark(secondMark)))
{
cout << "Thesecond mark is invalid!" << endl;
return;
}
if (!(IsValidMark(finalMark)))
{
cout << "Thefinal mark is invalid!" << endl;
return;
}
double total = firstMark + secondMark +finalMark;
char grade = GetGrade(total);
//Values
cout << studentNumber <<"\t" << id << "\t" << firstMark <<
"\t" <<
secondMark << "\t" << finalMark<< "\t" << total << "\t" << grade
<< endl;
studentNumber++; //if you want to callthis function one more time
}
bool IsValidMark(double mark)
{
if (mark > 0)
{
return true;
}
return false;
}
char GetGrade(double mark)
{
if (mark >= 90)
{
return 'A';
}
else if (mark >= 80)
{
return 'B';
}
else if (mark >= 70)
{
return 'C';
}
else if (mark >= 60)
{
return 'D'
}
else
{
return 'F';
}
}
=================================================================
=================================================================
That's all.
Also I'll give you object-oriented code:
In main file
class Student
{
static int studentNumber = 1; //staticmeans that variable will not recreates during all program time
int studentId;
double firstMark;
double secondMark;
double finalMark;
public:
//constructor with initialization string
Student (int studentId, doublefirstMark, double secondMark, double finalMark) :
studentId(studentId),firstMark(firstMark), secondMark(secondMark), finalMark(finalMark)
{ }
//destructor - it is called when classinstance is destroyed; it hasn't parameters
~Student()
{
studentNumber--;
}
public void ShowResult()
{
if(!(IsValidMark(firstMark))) //I've added one more brackets to make this line
more clearly, to avoid mistakes
{
cout<< "The first mark is invalid!" << endl;
return;
}
if(!(IsValidMark(secondMark)))
{
cout<< "The second mark is invalid!" << endl;
return;
}
if(!(IsValidMark(finalMark)))
{
cout<< "The final mark is invalid!" << endl;
return;
}
double total = firstMark+ secondMark + finalMark;
char grade =GetGrade(total);
//Values
cout <<studentNumber << "\t" << id << "\t"
<< firstMark << "\t" <<
secondMark << "\t"<< finalMark << "\t" << total <<
"\t" << grade << endl;
studentNumber++; //if youwant to call this function one more time
}
private: //these methods are private, because they are partsof realisation, so they must be incapsulated
char GetGrade(double mark)
{
if (mark >= 90)
{
return 'A';
}
else if (mark >= 80)
{
return 'B';
}
else if (mark >= 70)
{
return 'C';
}
else if (mark >= 60)
{
return 'D'
}
else
{
return 'F';
}
}
bool IsValidMark(double mark)
{
return mark > 0;
}
}
int main()
{
int studentId;
double firstMark;
double secondMark;
double finalMark;
cout << "Enter the srudentID: ";
cin >> studentId;
cout << "Enter the firstmark: ";
cin >> firstMark;
cout << "Enter the secondmark: ";
cin >> secondMark;
cout << "Enter the finalmark: ";
cin >> finalMark;
cout << "Theresults:\n";
//Headers
cout << "Student_Number"<< "\t" << "Student_Id" << "\t"
<< "First" << "\t" <<
"Second" << "\t" <<"Final" << "\t" << "Total" <<
"\t" << "Grade" << endl;
Student student(studentId, firstMark,secondMark, finalMark);
student.ShowResults();
return 0;
}
Comments
Leave a comment