Question

In: Computer Science

Below is my C++ program of a student record system. I have done the program through...

Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks.

#include<iostream>
#include<string>
using namespace std;
struct StudentRecord {
   string name;
   int roll_no;
   string department;
   StudentRecord *next;
};
StudentRecord *head = NULL;
StudentRecord *get_data()
{
   //creating a temporary node in which we will store all the student records and return the temporary node in the end of the function
   StudentRecord *rec = new StudentRecord;
   cout << endl;
   cout << "You chose option #1." << endl;
   cout << "What is the student's name?: ";
   cin >> rec->name;
   cout << "What is the student's roll no?: ";
   cin >> rec->roll_no;
   cout << "What is the student's department?: ";
   cin >> rec->department;
   rec->next = head;
   return rec;
}

void add_data(StudentRecord *current)
{
   // We will store the address of the present head node in the next field of the current node and later we will make the current node as head node
   current->next = head; // store the address of the pointer head(second field)
   head = current;

}
void display_data(double key)
{
   cout << endl << endl << "Student record: " << endl;
   cout << "---------------------------" << endl;
   // We will create a node named start and will iterate it through the whole linked list and display the data
   StudentRecord *start = head;
   if (!start) {
       cout << "No Data!" << endl;
       return;
   }
   while (start != NULL)
   {
       if (start->roll_no == key)
       {
           cout << "Name : "<<start->name << endl;
           cout << "Roll no : "<<start->roll_no << endl;
           cout << "Department : "<<start->department << endl;
           return;
       }
       start = start->next;
   }
   cout << "Record not found" << endl;
}
void search(double key)
{
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (head != NULL)
   {
       if (head->roll_no == key)
       {
           cout << "key found" << endl;
           // cout<<head->uin<<endl;
           cout << "name = " << head->name << endl;
           cout << "department = " << head->department << endl;
           return;
       }
       head = head->next;
   }
   cout << "Key not found" << endl;
}
void UpdateData(double key)
{
   StudentRecord *temp;
   temp = head;
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (temp != NULL)
   {
       if (temp->roll_no == key)
       {
           cout << "What is the student's name?: ";
           cin >> temp->name;
           cout << "What is the student's uin?: ";
           cin >> head->roll_no;
           cout << "What is the student's date of birth?: ";
           cin >> temp->department;
           cout << "Record updated..." << endl;
           return;
       }
       temp = temp->next;
   }
   cout << "Key not found to update" << endl;
}
void DeleteRecord(double key)
{
   StudentRecord *temp1, *temp2, *temp;
   temp = head;
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (temp != NULL)
   {

       if (temp->roll_no == key)

       {

           head = temp->next;

           cout << "Record Deleted successfully...." << endl;

           return;

       }
       if (temp->next->roll_no == key)
       {
           temp1 = temp->next;
           temp2 = temp1->next;
           temp->next = temp2;
           cout << "Record Deleted Successfully..." << endl;
           return;
       }
       temp = temp->next;
   }
   cout << "Key not found to Delete Record" << endl;
}
void processMenu()
{
   // creating current node for StudentRecord struct
   StudentRecord *current = NULL;
   int ser;
   char choice = 0;
   while (1) {
       cout << endl << "What would you like to do?" << endl;
       cout << "==========================" << endl;
       cout << "1. Enter a student record. " << endl;
       cout << "2. Display student records. " << endl;
       cout << "3. Search. " << endl;
       cout << "4. Update. " << endl;
       cout << "5. Delete. " << endl;
       cout << "6. Exit. " << endl;
       cin >> choice;
       while (cin.get() != '\n');
       if (choice == '1') {
           system("cls");
           current = get_data();
           add_data(current);
       }
       else if (choice == '2') {
           cout << "Enter student uin to Display records" << endl;
           cin >> ser;
           display_data(ser);
       }
       else if (choice == '3') {
           system("cls");
           cout << "Enter student uin to search for records" << endl;
           cin >> ser;
           search(ser);
       }
       else if (choice == '4') {
           system("cls");
           cout << "Enter student uin to Update records" << endl;
           cin >> ser;
           UpdateData(ser);
       }
       else if (choice == '5') {
           system("cls");
           cout << "Enter student uin to Delete records" << endl;
           cin >> ser;
           DeleteRecord(ser);
       }
       else if (choice == '6') {
           exit(1);
           return;
       }
       else {
           cout << "Allowed Selections are 1, 2, 3, 4, 5, and 6." << endl;
       }
   }
}

int main()
{
   // Program starts execution from main block
   cout << "Student Record Program." << endl << endl;
   processMenu();
   return 0;
}

Solutions

Expert Solution

Note: replaced struct with class and made all data members public

wherever I changed print statement are marked as //Changed here

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class StudentRecord { //replaced struct with class and made all data variables public
public:
   string name;
   int roll_no;
   string department;
   StudentRecord *next;
};
StudentRecord *head = NULL;
StudentRecord *get_data()
{
   //creating a temporary node in which we will store all the student records and return the temporary node in the end of the function
   StudentRecord *rec = new StudentRecord;
   cout << endl;
   //cout << "You chose option #1." << endl; Changed here
   cout << "What is the student's name?: ";
   cin >> rec->name;
   cout << "What is the student's roll no?: ";
   cin >> rec->roll_no;
   cout << "What is the student's department?: ";
   cin >> rec->department;
   rec->next = head;
   system("cls");//changed here
   return rec;
}

void add_data(StudentRecord *current)
{
   // We will store the address of the present head node in the next field of the current node and later we will make the current node as head node
   current->next = head; // store the address of the pointer head(second field)
   head = current;

}
void display_data(double key)
{
   cout << endl << endl << "Student record: " << endl;
   cout << "---------------------------" << endl;
   // We will create a node named start and will iterate it through the whole linked list and display the data
   StudentRecord *start = head;
   if (!start) {
       cout << "No Data!" << endl;
       return;
   }
   while (start != NULL)
   {
       if (start->roll_no == key)
       {
           cout << "Name : "<<start->name << endl;
           cout << "Roll no : "<<start->roll_no << endl;
           cout << "Department : "<<start->department << endl;
           cout << "---------------------------" << endl;
           return;
       }
       start = start->next;
   }
   cout << "Record not found" << endl;
   cout << "---------------------------" << endl;
}
void search(double key)
{
    cout << "---------------------------" << endl;
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (head != NULL)
   {
       if (head->roll_no == key)
       {
           cout << "Record found" << endl;//changed here
           // cout<<head->uin<<endl;
           cout << "name = " << head->name << endl;
           cout << "department = " << head->department << endl;
           cout << "---------------------------" << endl;
           return;
       }
       head = head->next;
   }
   cout << "Record not found" << endl;//changed here
   cout << "---------------------------" << endl;
}
void UpdateData(double key)
{
   StudentRecord *temp;
   temp = head;
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (temp != NULL)
   {
       if (temp->roll_no == key)
       {
           cout << "What is the student's name?: ";
           cin >> temp->name;
           cout << "What is the student's roll number?: ";
           cin >> head->roll_no;
           cout << "What is the student's department?: ";//changed here
           cin >> temp->department;
           cout << "Record updated..." << endl;
           return;
       }
       temp = temp->next;
   }
   cout << "Record not found to update" << endl;//changed here
}
void DeleteRecord(double key)
{
   StudentRecord *temp1, *temp2, *temp;
   temp = head;
   // We will iterate the head through the linked list until it finds the required variable or until the end of linked list
   while (temp != NULL)
   {

       if (temp->roll_no == key)

       {

           head = temp->next;

           cout << "Record Deleted successfully...." << endl;

           return;

       }
       if (temp->next->roll_no == key)
       {
           temp1 = temp->next;
           temp2 = temp1->next;
           temp->next = temp2;
           cout << "Record Deleted Successfully..." << endl;
           return;
       }
       temp = temp->next;
   }
   cout << "Record not found to Delete Record" << endl;//Changed here
}
void processMenu()
{
   // creating current node for StudentRecord
   StudentRecord *current = NULL;
   int ser;
   char choice = 0;
   while (1) {
       cout << "\n\t==========================" << endl;//added tab space
       cout << "\t1. Enter a student record. " << endl;
       cout << "\t2. Display student records. " << endl;
       cout << "\t3. Search student record " << endl;
       cout << "\t4. Update student record " << endl;
       cout << "\t5. Delete student record " << endl;
       cout << "\t6. Exit. " << endl;
       cout << "\t==========================" << endl;
       cout<<"\n\tPlease Enter Your Choice (1-6): ";
       cin >> choice;
       while (cin.get() != '\n');
       if (choice == '1') {
           system("cls");
           current = get_data();
           add_data(current);
       }
       else if (choice == '2') {
            system("cls");//changed here
           cout << "Enter student roll number to Display record" << endl;//changed here
           cin >> ser;
           display_data(ser);
       }
       else if (choice == '3') {
           system("cls");
           cout << "Enter student roll number to search for records" << endl;//changed here
           cin >> ser;
           search(ser);
       }
       else if (choice == '4') {
           system("cls");
           cout << "Enter student roll number to Update records" << endl;//changed here
           cin >> ser;
           UpdateData(ser);
       }
       else if (choice == '5') {
           system("cls");
           cout << "Enter student roll number to Delete records" << endl;//changed here
           cin >> ser;
           DeleteRecord(ser);
       }
       else if (choice == '6') {
           exit(1);
           return;
       }
       else {
           cout << "Allowed Selections are 1, 2, 3, 4, 5, and 6." << endl;
       }
   }
}

int main()
{
   // Program starts execution from main block
   cout << "Student Record Program." << endl << endl;
   processMenu();
   return 0;
}

Output of main menu:

Student Record Program.


==========================
1. Enter a student record.
2. Display student records.
3. Search student record
4. Update student record
5. Delete student record
6. Exit.
==========================

Please Enter Your Choice (1-6):


Related Solutions

Can you please see what I have done wrong with my program code and explain, This...
Can you please see what I have done wrong with my program code and explain, This python program is a guess my number program. I can not figure out what I have done wrong. When you enter a letter into the program, its supposed to say "Numbers Only" as a response. I can not seem to figure it out.. instead of an error message. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if...
In C#, I have 6 teaching positions. I have to keep my salary expenses below $250,000...
In C#, I have 6 teaching positions. I have to keep my salary expenses below $250,000 and each position carries insurance and tax costs equal to 25% of their salary. Create an application that allows me to enter the Years of Experience for 6 teacher positions. Based on those years of experience, look up the minimum salary, total the salaries for all 6 positions. Calculate the 25% tax and insurance expense based on the salaries, and display the total of...
I have a programming project due tonight that I have already done once but my professor...
I have a programming project due tonight that I have already done once but my professor sent back because he said I did it wrong. He wants the following using linked lists but i only know how to do it with Arrays. Please try to explain your answer to me and not just give it to me. For this assignment you must write a program that implements a stack of integers. The stack should be implemented using classes and must...
Hello I need a small fix in my program. I need to display the youngest student...
Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
In the C# programming language... Write a program to perform student record manage for class IST311....
In the C# programming language... Write a program to perform student record manage for class IST311. Create student record class (Student.cs) and it should get the student information from the user and set up student record class. The program will perform recording student’s grade information and compute final grade, and then print out the student’s class information. Student class definitions: It should contain attributes as following: first name, last name, 5 labs grade, 3 grade, final grade. Its member functions...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
I have done some of this on my own but just cant seem to be able...
I have done some of this on my own but just cant seem to be able to finish this correctly. Dual-energy X-ray absorptiometry (DXA) is a technique for measuring bone health. One of the most common measures is total body bone mineral content (TBBMC). A highly skilled operator is required to take the measurements. Recently, a new DXA machine was purchased by a research lab, and two operators were trained to take the measurements. TBBMC for eight subjects was measured...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you. so easentially I need someone to write some unit tests with my program in MSTest visual studio 2019. Then I need the unit tests supplied as answers. using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void...
Below is a school problem of mine. WHAT I KNOW AND HAVE DONE. i have three...
Below is a school problem of mine. WHAT I KNOW AND HAVE DONE. i have three variables for input n for nunber of wnemies k for fight capacity an arraylist set to the size of n because it only needs to be as large as the number of enemies coming. and x which is just the time stamps that will go into the arraylist. i also have tHe array sorted from least to greatest because it doesnt matter what order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT