Write a menu based program to maintain student records. Your program should take
ID: 657525 • Letter: W
Question
Write a menu based program to maintain student records. Your program should take the following inputs:
1. Student first name (max. 20 characters)
2. Student last name, (max. 20 characters)
3. Student scores (float/double), eg. 85.4
4. Zip code (may be a string)
You have to create a structure Student with above information. Then create a linked list where each list node contains a Student. Your program should be able to take records of a minimum of 5 students. After taking the records, you should provide following functionalities to the user.
1. Print records
Explanation / Answer
Program code:
// Students-LinkedList.cpp : Defines the entry point for the console application.
//Header files
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
static int count=0;
struct Student
{
char first_name[20];
char last_name[20];
float score;
char zipcode[20];
struct Student *next;
};
//definition of printRecords that prints the each record details
void printRecords(struct Student *head)
{
struct Student *temp=head;
cout<<" Student details are: "<<endl<<endl;
//repeat until pointer reaches the null value
while(temp!=NULL)
{
//print each student record details
cout<<"First Name: "<<temp->first_name<<endl;
cout<<"Last Name: "<<temp->last_name<<endl;
cout<<"Student score: "<<temp->score<<endl;
cout<<"Zip code: "<<temp->zipcode<<endl<<endl;
//goto next node like incrementing the pointer
temp = temp->next;
}
}
//definition of addStudentRecord that adds a student details
void addStudentRecord(struct Student **head)
{
struct Student *stud,*temp;
//create a memory for the student structure
stud=(struct Student*)malloc(sizeof(struct Student));
//prompt the user to enter the values
cout<<" Enter First name: ";
cin>>stud->first_name;
cout<<"Enter last name: ";
cin>>stud->last_name;
cout<<"Enter student score: ";
cin>>stud->score;
cout<<"Enter zip code: ";
cin>>stud->zipcode;
//if it is a first element in the list
if(*head==NULL)
{
*head=stud;
(*head)->next=NULL;
}
////Otherwise add the new node to the end of the list
else
{
temp=*head;
//loop till the pointer temp reaches the NULL value
while(temp->next!=NULL)
temp=temp->next;
//Add the node to the end of the list
temp->next=stud;
stud->next=NULL;
}
}
//definition of deleteStudentRecords function that deletes the
//record containing the last name entered by the user
void deleteStudentRecords(struct Student **head)
{
struct Student *temp1=*head,*temp2;
char lastname[20];
//prompt the user to enter the last name
cout<<" Enter last name to delete student : ";
cin>>lastname;
//loop till the end of the list
while(temp1!=NULL)
{
temp2=temp1;
//condition to check whether the last names are equal or not
//if equal delete the node by calling the free function
if(strcmp(temp1->last_name,lastname)==0)
{
//Link prior and next node of t1
temp2->next=temp1->next;
temp1=temp2->next;
delete temp2;
}
//move the pointer
temp1=temp1->next;
}
}
//main function
int main()
{
struct Student *head=NULL;
int choice=1;
while(choice!=0)
{
cout<<endl;
cout<<"*****Menu*****"<<endl;
cout<<" 1.Print records 2. Add a new record 3. Delete record(s) 4. Exit ";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
printRecords(head);
break;
case 2:
addStudentRecord(&head);
break;
case 3:
deleteStudentRecords(&head);
break;
case 4:
cout<<" Bye..";
system("pause");
break;
default:
printf(" Invalid choice try again");
}
}
return 0;
}
Sample Output:
*****Menu*****
1.Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 2
Enter First name: Henry
Enter last name: Leo
Enter student score: 78.9
Enter zip code: 2345
*****Menu*****
1.Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 2
Enter First name: Finn
Enter last name: Rose
Enter student score: 76.90
Enter zip code: 23456
*****Menu*****
1.Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 2
Enter First name: Kelvin
Enter last name: Thimothy
Enter student score: 89.0
Enter zip code: 87654
*****Menu*****
1.Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 2
Enter First name: William
Enter last name: Penny
Enter student score: 56.87
Enter zip code: 12876
*****Menu*****
1.Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 2
Enter First name: Kelvin
Enter last name: Martin
Enter student score: 76.56
Enter zip code: 34567
*****Menu*****
1. Print records
2. Add a new record
3. Delete record(s)
4. Exit
Enter your choice: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.