Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

#include <iostream>

using namespace std;

struct PersonInfo {
int id;
string name;
};

struct Student {
PersonInfo person;
int year;
double gpa;
};

void displayMenu() {
cout << "Menu" << endl;
cout << "1. Input" << endl;
cout << "2. Output" << endl;
cout << "3. Update"<<endl;
cout << "4. Delete"<<endl;
cout << "5. Search"<<endl;
cout << "6. Sort"<<endl;
cout << "0. Exit" << endl;

int getChoice() {
int choice;
cout << "Enter choice:";
cin >> choice;
return choice;
}

Student getStudent() {
Student s;
cout << "Enter ID:";
cin >> s.person.id;
cout << "Enter Name:";
cin >> s.person.name;
cout << "Enter Year:";
cin >> s.year;
cout << "Enter GPA:";
cin >> s.gpa;
return s;
}

void inputStudents(Student s[], int num,int& count) {


cout << "Input Student" << endl;
for (int i = 0; i < num; i++) {
s[count++] = getStudent();
}
}

void outputStudents(Student s[], int count) {


cout << "Output Students" << endl;
for (int i = 0; i < count; i++) {
cout << "ID:" << s[i].person.id << " Name:" << s[i].person.name << " Year:"
<< s[i].year << " GPA:" << s[i].gpa << endl;
}
}
void displayStudent(Student s){
cout<<"1. ID:"<<s.person.id<<endl;
cout<<"2. Name:"<<s.person.name<<endl;
cout<<"3. Year:"<<s.year<<endl;
cout<<"4. GPA:"<<s.gpa<<endl;
}
int findStudentById(Student s[], int count, int id){
for(int i=0;i<count;i++){
if(s[i].person.id==id){
return i;
}
}
return -1;
}
void updateStudentsById(Student s[], int count,int id){
int option;
int index=findStudentById(s,count,id);
if(index!=-1){
displayStudent(s[index]);
option=getChoice();
switch(option){
case 1:
cout<<"Enter new id:";
cin>>s[index].person.id;
break;
case 2:
cout<<"Enter new name:";
cin>>s[index].person.name;
break;
case 3:
cout<<"Enter new year:";
cin>>s[index].year;
break;
case 4:
cout<<"Enter new gpa:";
cin>>s[index].gpa;
break;
}
cout<<"Updated successfully"<<endl;
}else{
cout<<"Not found"<<endl;
}

}
void updateStudentsByName(Student s[], int count,string name){
int option;
for(int i=0;i<count;i++){
if(s[i].person.name==name){
displayStudent(s[i]);
option=getChoice();
switch(option){
case 1:
cout<<"Enter new id:";
cin>>s[i].person.id;

break;
case 2:
cout<<"Enter new name:";
cin>>s[i].person.name;
break;
case 3:
cout<<"Enter new year:";
cin>>s[i].year;
break;
case 4:
cout<<"Enter new gpa:";
cin>>s[i].gpa;
break;
}
cout<<"Updated successfully"<<endl;
break;
}
}
}
void deleteStudentsById(Student s[], int &count, int id){
char ch;
int index=findStudentById(s,count,id);
if(index!=-1){
displayStudent(s[index]);
cout<<"Are you sure to delete?(y/n)";
cin>>ch;
if(ch=='y'){
for(int j=index;j<count-1;j++){
s[j]=s[j+1];
}
count--;
cout<<"Deleted successfully"<<endl;
}
}else{
cout<<"Not found"<<endl;
}
}

void searchStudentsById(Student s[], int count, int id){


int index=findStudentById(s,count,id);
if(index!=-1){
displayStudent(s[index]);
}else{
cout<<"Not found"<<endl;
}
}

void sortStudentById(Student s[], int count, int option){


switch(option){
case 1:
for(int i=0;i<count;i++){
for(int j=0;j<count-i-1;j++){
if(s[j].person.id>s[j+1].person.id){
Student st;
st=s[j];
s[j]=s[j+1];
s[j+1]=st;
}

}
}
break;
case 2:
for(int i=0;i<count;i++){
for(int j=0;j<count-i-1;j++){
if(s[j].person.id<s[j+1].person.id){
Student st;
st=s[j];
s[j]=s[j+1];
s[j+1]=st;
}

}
}

break;
}
cout<<"Sorted successfully"<<endl;
}

void performMenuChoice(int choice, Student s[], int &count) {


enum OPTION {EXIT, INPUT, OUTPUT, UPDATE, DELETE, SEARCH, SORT };
switch (choice) {
case INPUT: {
int num;
cout << "How many students?";
cin >> num;
inputStudents(s, num, count);
break;
}
case OUTPUT:
outputStudents(s, count);
break;
case UPDATE:{
int option;
cout<<"1.Update by id"<<endl;
cout<<"2.Update by Name"<<endl;
option=getChoice();
switch(option){
case 1:{
int id;
cout<<"Enter id:";
cin>>id;
updateStudentsById(s,count,id);
break;
}
case 2:{
string name;
cout<<"Enter name:";
cin>>name;
updateStudentsByName(s,count,name);
break;
}
}
break;
}
case DELETE:{
int option;
cout<<"1.Delete by id"<<endl;
cout<<"2.Delete by Name"<<endl;
option=getChoice();
switch(option){
case 1:{
int id;
cout<<"Enter id:";
cin>>id;
deleteStudentsById(s,count,id);
break;
}
case 2:{
string name;
cout<<"Enter name:";
cin>>name;
//deleteStudentsByName(s,count,name);
break;
}
}
}
break;

case SEARCH:{
int option;
cout<<"1.Search by id"<<endl;
cout<<"2.Search by Name"<<endl;
option=getChoice();
switch(option){
case 1:{
int id;
cout<<"Enter id:";
cin>>id;
searchStudentsById(s,count,id);
break;
}
case 2:{

break;
}
}
break;
}
case SORT:{
int option;
cout<<"1.Sort by id"<<endl;
cout<<"2.Sort by Name"<<endl;
option=getChoice();
switch(option){
case 1:{
cout<<"1.Ascending"<<endl;
cout<<"2.Descending"<<endl;
option=getChoice();
sortStudentById(s,count,option);
break;
}
case 2:{

break;
}
}
break;
}
case EXIT:
exit(0);
default:
cout << "Invalid" << endl;
break;
}
}

int main()
{
const int SIZE = 100;
Student s[SIZE];
int count = 0;
int choice;

do {
displayMenu();
choice=getChoice();
performMenuChoice(choice, s, count);
} while (choice != 0);

return 0;
}

You might also like