Вы находитесь на странице: 1из 6

#include <iostream>

#include <string>
#include <iomanip>

using namespace std;

//declaring student structure


struct student
{

string stnumber;
string stname;
char sex;
float quizz1;
float quizz2;
float assigment;
float midterm;
float final;
float total;
int numberOfitem;
};

void displaymenu();
int search(student st[], string , int s);
void add_rec(student st[], int&);
void viewall(student st[], int);
void average(student st[], int);

//the main function


void main()
{
student st[80];
int itemcount = 0;
string id;

//show menu
int yourchoice;
char confirm;
do
{
displaymenu();
cout << "Enter your choice(1-4):";
cin >> yourchoice;

switch (yourchoice){
case 1:add_rec(st, itemcount); break;
case 2:
{
cout << "\nEnter student's ID:";
cin >> id;
search(st, id, itemcount); break;
}
case 3:average(st, itemcount); break;
case 4:viewall(st, itemcount); break;

default:cout << "invalid";


}

cout << "Press y or Y to continue: ";


cin >> confirm;
} while (confirm == 'y' || confirm == 'Y');

}
//function to display a menu of choices
void displaymenu(){
cout <<
"==========================================" << "\n";
cout << " MENU " << "\n";
cout <<
"==========================================" << "\n";
cout << " 1.Add student records" << "\n";
cout << " 2.Find a student by ID" << "\n";
cout << " 3.Calculate average score of a student" << "\n";
cout << " 4.View all student records" << "\n";

//function to find the location of the record


int search(student st[], string id, int itemcount){
int found = -1;
for (int i = 0; i < itemcount && found == -1; i++)
{

if (st[i].stnumber == id) found = i;

else found = -1;


}

return found;
}

//function to append a new record


void add_rec(student st[], int& itemcount){

cout << "\nEnter student's ID:";


cin >> st[itemcount].stnumber;
while (search(st, st[itemcount].stnumber, itemcount) != -1)
{
cout << "This ID already exists\n";
cout << "\nEnter student's ID:";
cin >> st[itemcount].stnumber;
}

cout << "Enter student's Name:";


cin >> st[itemcount].stname;
cout << "Enter student's Sex(F or M):"; cin >> st[itemcount].sex;
cout << "Enter student's quizz1 score:"; cin >>
st[itemcount].quizz1;
cout << "Enter student's quizz2 score:"; cin >>
st[itemcount].quizz2;
cout << "Enter student's assigment score:"; cin >>
st[itemcount].assigment;
cout << "Enter student's mid term score:"; cin >>
st[itemcount].midterm;
cout << "Enter student's final score:"; cin >> st[itemcount].final;
st[itemcount].total = st[itemcount].quizz1 + st[itemcount].quizz2 +
st[itemcount].assigment + st[itemcount].midterm + st[itemcount].final;

++itemcount;

//function to view all records


void viewall(student st[], int itemcount){
int i = 0;
cout << left << setw(5) << "ID" << setw(20) << "NAME" <<
setw(5) << "SEX"

<< setw(5) << "Q1"


<< setw(5) << "Q2" << setw(5) << "As" << setw(5) << "Mi"
<< setw(5) << "Fi"
<< setw(5) << "TOTAL" << "\n";
cout <<
"==============================================\n";
while (i <= itemcount){
if (st[i].stnumber != ""){
cout << left << setw(5) << st[i].stnumber << setw(20)
<< st[i].stname << setw(5)

<< st[i].sex;
cout << setw(5) << st[i].quizz1 << setw(5) <<
st[i].quizz2 << setw(5) << st[i].assigment
<< setw(5) << st[i].midterm << setw(5) <<
st[i].final << setw(5)
<< st[i].total;

cout << "\n";


}
i = i + 1;

}
//function to calculate the average score of a student
void average(student st[], int itemcount)
{
string id;
float avg = 0;
cout << "Enter students'ID:";
cin >> id;
int index = search(st, id, itemcount);
if (index != -1 && itemcount>0)
{
st[index].total = st[index].quizz1 + st[index].quizz2 +
st[index].assigment

+ st[index].midterm + st[index].final;
avg = st[index].total / 5;
}

cout << "The average score is " << avg;


}

Вам также может понравиться