Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Node
- {
- string name;
- int grade;
- Node *next;
- };
- void append(Node **head_ref, string new_name, int new_grade);
- void equalGrade(Node *node, int grade);
- void greaterThanGrade(Node *node, int grade);
- void lessThanGrade(Node *node, int grade);
- void printList(Node *node);
- int main()
- {
- Node *head = NULL;
- string name = " ";
- int grade = 0;
- bool valid = false;
- char entry = ' ';
- bool quit = false;
- char choice = ' ';
- int gradeCheck = 0;
- while(!quit)
- {
- cout << "Enter name: ";
- getline(cin, name);
- valid = false;
- while(!valid)
- {
- cout << "Enter grade: ";
- cin >> grade;
- if(cin.good() && grade > 0) valid = true;
- else
- {
- cout << "Invalid grade!" << endl;
- cin.clear();
- cin.ignore();
- }
- }
- append(&head, name, grade);
- valid = false;
- while(!valid)
- {
- cout << "Another entry(Y/N)? ";
- cin >> entry;
- if(toupper(entry) == 'N')
- {
- valid = true;
- quit = true;
- }
- else if (toupper(entry) == 'Y')
- {
- valid = true;
- cin.clear();
- cin.ignore();
- }
- else
- {
- cin.clear();
- cin.ignore();
- }
- cout << "\n";
- }
- }
- while(true)
- {
- cout << "Criteria for displaying the list:\n";
- cout << "A. Grade =\n";
- cout << "B. Grade >\n";
- cout << "C. Grade <\n";
- cout << "D. All\n";
- valid = false;
- while(!valid)
- {
- cout << "Enter choice(A-D): ";
- cin >> choice;
- if(toupper(choice) == 'A' || toupper(choice) == 'B' || toupper(choice) == 'C')
- {
- valid = false;
- while(!valid)
- {
- cout << "Enter grade: ";
- cin >> gradeCheck;
- if(cin.good() && gradeCheck > 0)
- {
- cout << "\n";
- valid = true;
- }
- else
- {
- cout << "Invalid grade!" << endl;
- cin.clear();
- cin.ignore();
- }
- }
- }
- else if(toupper(choice) == 'D') valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- switch(toupper(choice))
- {
- case 'A':
- equalGrade(head, gradeCheck);
- break;
- case 'B':
- greaterThanGrade(head, gradeCheck);
- break;
- case 'C':
- lessThanGrade(head, gradeCheck);
- break;
- case 'D':
- printList(head);
- break;
- default:
- cout << "Invalid input!\n";
- }
- }
- return 0;
- }
- void append(Node **head_ref, string new_name, int new_grade)
- {
- Node *new_node = new Node();
- Node *last = *head_ref;
- new_node->name = new_name;
- new_node->grade = new_grade;
- new_node->next = NULL;
- if(*head_ref == NULL)
- {
- *head_ref = new_node;
- return;
- }
- while(last->next != NULL) last = last->next;
- last->next = new_node;
- return;
- }
- void equalGrade(Node *node, int grade)
- {
- while(node != NULL)
- {
- if(grade == node->grade)
- {
- cout << "Name: " << node->name << endl;
- cout << "Grade: " << node->grade << endl;
- cout << "\n";
- }
- node = node->next;
- }
- }
- void greaterThanGrade(Node *node, int grade)
- {
- while(node != NULL)
- {
- if(grade < node->grade)
- {
- cout << "Name: " << node->name << endl;
- cout << "Grade: " << node->grade << endl;
- cout << "\n";
- }
- node = node->next;
- }
- }
- void lessThanGrade(Node *node, int grade)
- {
- while(node != NULL)
- {
- if(grade > node->grade)
- {
- cout << "Name: " << node->name << endl;
- cout << "Grade: " << node->grade << endl;
- cout << "\n";
- }
- node = node->next;
- }
- }
- void printList(Node *node)
- {
- while(node != NULL)
- {
- cout << "Name: " << node->name << endl;
- cout << "Grade: " << node->grade << endl;
- cout << "\n";
- node = node->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement