0% found this document useful (0 votes)
32 views4 pages

Sms

The document defines a Student struct containing personal information like ID and name, as well as year and GPA. It includes functions to input, output, update, and delete student records from an array. The main function displays a menu and calls performMenuChoice to handle user input, allowing them to manage an array of student data by inputting, viewing, updating, or deleting records.

Uploaded by

Leon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Sms

The document defines a Student struct containing personal information like ID and name, as well as year and GPA. It includes functions to input, output, update, and delete student records from an array. The main function displays a menu and calls performMenuChoice to handle user input, allowing them to manage an array of student data by inputting, viewing, updating, or deleting records.

Uploaded by

Leon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#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 << "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;
}
void updateStudentsById(Student s[], int count,int id){
int option;
for(int i=0;i<count;i++){
if(s[i].person.id==id){
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;
}
break;
}
}
}
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;
}
break;
}
}
}
void deleteStudentsById(Student s[], int &count, int id){
for(int i=0;i<count;i++){
if(s[i].person.id==id){
for(int j=i;j<count-1;j++){
s[j]=s[j+1];
}
count--;
break;
}
}
}

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


enum OPTION {EXIT, INPUT, OUTPUT, UPDATE, DELETE };
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 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