5.2 The program should be menu driven, with the options: Capture Student Marks, Sort the Class List, Display the Class List, Exit.
#include <iostream>
class student
{
int mark;
std::string name;
public:
student(int m, const std::string& n)
{
this->mark = m;
this->name = n;
}
void print()
{
std::cout<<"name: "<<this->name<<" has mark "<<this->mark<<std::endl;
}
const int& get_mark() const
{
return this->mark;
}
const std::string& get_name() const
{
return this->name;
}
};
bool compare_names(const student& s1,const student& s2)
{
if(s1.get_name() < s2.get_name())
{
return true;
}
else return false;
}
void Capture_marks(student* s, int size)
{
for(int i = 0; i != size; ++i)
{
std::cout<<i<<'\t'<<s[i].get_mark()<<std::endl;
}
}
void Print_sorted(student* s, int size)
{
std::sort(s, s + size, compare_names);
for(int i = 0; i != size; ++i)
{
s[i].print();
}
}
void Print(student* s, int size)
{
for(int i = 0; i != size; ++i)
{
s[i].print();
}
}
int main()
{
bool exit = false;
int choice = 0;
student students[10] = {{67, "Alvaro Nielsen"},{45, "Dean Hinton"},{17, "Emilio Pitts"},{89, "Samara Baldwin"},{98, "Hannah Myers"},{67, "Kate Patton"},{63, "Colten Bowman"},{79, "Cornelius Krueger"},{96, "Jaylen Fleming"},{94, "Leslie Chaney"}};
while(!exit)
{
std::cout<<"Please enter a digit of your choice\n"<<
"1. Capture student marks\n"<<
"2. Print sorted student list\n"<<
"3. Display class list\n"<<
"4. Exit\n";
std::cin>>choice;
switch(choice)
{
case 1:
Capture_marks(students, 10);
break;
case 2:
Print_sorted(students, 10);
break;
case 3:
Print(students, 10);
break;
case 4:
exit = true;
break;
default:
continue;
break;
}
}
return 0;
}
Comments
Leave a comment