write the programme in c++ that do so mainy thing: 1) Search for student by ID:s
ID: 3637231 • Letter: W
Question
write the programme in c++ that do so mainy thing:1) Search for student by ID:system asks for student ID, if ID is valid, student’s name and his grade are displayed.
2)Search for Student by name:systems asks for student’s name, then it displays all students with that name, it displays ID, name, grades.
3)Search for students according to the average:system asks user to enter an average then it displays students whose average is equal or greater than that number (displays id, name, all grades, average).
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct student {
string name;
int id;
int grade;
student* next;
};
void AddStudent(student* &list) {
string _name;
int _id;
int _grade;
cout << "Enter the student's name: ";
cin >> _name;
cout << "Enter the student's ID: ";
cin >> _id;
cout << "Enter the student's grade: ";
cin >> _grade;
cout << endl;
student* temp = new student;
temp->name = _name;
temp->id = _id;
temp->grade = _grade;
temp->next = list;
list = temp;
cout << "Student added. ";
system("pause");
};
void SearchByName(student* list) {
if (list == NULL) cout << "There are no students in current database. ";
else {
string _name;
cout << "Enter the name: ";
cin >> _name;
cout << endl;
bool found = false;
while (list!=NULL) {
if (list->name == _name) {
cout << "Name: " << list->name << " ID: " << list->id << " Grade: " << list->grade << endl << endl;
found = true;
};
list = list->next;
};
if (!found) cout << "No students found. ";
};
system("pause");
};
void SearchByID(student* list) {
if (list == NULL) cout << "There are no students in current database. ";
else {
int _id;
cout << "Enter the ID: ";
cin >> _id;
cout << endl;
bool found = false;
while (list!=NULL) {
if (list->id == _id) {
cout << "Name: " << list->name << " ID: " << list->id << " Grade: " << list->grade << endl << endl;
found = true;
};
list = list->next;
};
if (!found) cout << "No students found. ";
};
system("pause");
};
void SearchByAverage(student* list) {
if (list == NULL) cout << "There are no students in current database. ";
else {
int _grade;
cout << "Enter the average: ";
cin >> _grade;
cout << endl;
bool found = false;
cout << "Students whose grade is equal or greater: ";
while (list!=NULL) {
if (list->grade >= _grade) {
cout << "Name: " << list->name << " ID: " << list->id << " Grade: " << list->grade << endl << endl;
found = true;
};
list = list->next;
};
if (!found) cout << "No students found. ";
};
system("pause");
};
int MenuChoice() {
cout << "Choose your action: 1) Enter new student 2) Search student by ID 3) Search student by name " <<
"4) Search students by average 5) Exit ";
int a;
cin >> a;
cout << endl;
return a;
};
int main() {
student* list = new student;
list = NULL;
bool flag = true;
while (flag) {
switch (MenuChoice()) {
case 1:
AddStudent(list);
break;
case 2:
SearchByID(list);
break;
case 3:
SearchByName(list);
break;
case 4:
SearchByAverage(list);
break;
case 5:
flag = false;
break;
};
};
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.