*** I need 3 things done to this program or needs to be fixed. (1) The modify fu
ID: 3721616 • Letter: #
Question
*** I need 3 things done to this program or needs to be fixed.
(1) The modify function doesnt work, when i try to modify the students info it doesnt do anything i need that fixed.
(2) The delete function doesnt work, when i try to delete a students info it only deletes the name and still leaves the ID number and GPA, i need it to delete everything from that particular student.
(3) Also i need it so that if the list is empty and i ask to display the list, it should say "Linked List is empty" Here is the code:
#include<iostream>
#include<conio.h>
#include<Windows.h>
#include<fstream>
using namespace std;
bool check = true;
struct node
{
char firstname[20];
char lastname[20];
int idNum;
float gpa;
node *next;
}*head,*lastptr;
void Display(){
cout<<"Linked list data is:"<<endl;
node *temp=head;
while(temp!=NULL)
{
cout<<" *** ID NUMBER: "<<temp->idNum<<" *** NAME: "<<temp->firstname<<" "<<temp->lastname<<" *** GPA: "<<temp->gpa<<endl;
if(temp->next!=NULL)
cout<<"";
temp=temp->next;
}
}
void Add() //Add Student's Info//
{
node *p;
p=new node;
cout<<"Enter name of student:"<<endl;
cin>>p->firstname;
cin>>p->lastname;
fflush(stdin);
cout<<"Enter Id Number of student:"<<endl;
cin>>p->idNum;
fflush(stdin);
cout<<"Enter GPA of student:"<<endl;
cin>>p->gpa;
fflush(stdin);
p->next=NULL;
if(check)
{
head = p;
lastptr = p;
check = false;
}
else
{
lastptr->next=p;
lastptr=p;
}
cout<<endl<<"Student's Info is in the List ";
getch();
}
void Modify() //Modifies Student's Info//
{
node *ptr;
node *prev=NULL;
node *current=NULL;
int id_Num;
cout<<"Enter Id Number to search:"<<endl;
cin>>id_Num;
prev=head;
current=head;
while(current->idNum!=id_Num)
{
prev=current;
current=current->next;
}
ptr=new node;
fflush(stdin);
cout<<"Enter name of student:"<<endl;
gets(ptr->firstname);
gets(ptr->lastname);
fflush(stdin);
cout<<"Enter ID number of student:"<<endl;
cin>>ptr->idNum;
fflush(stdin);
cout<<"Enter GPA of student:"<<endl;
cin>>ptr->gpa;
fflush(stdin);
prev->next=ptr;
ptr->next=current->next;
current->next=NULL;
delete current;
cout<<endl<<"Student's Info is Changed ";
getch();
}
void Search() //Searches Student's Info//
{
node *prev=NULL;
node *current=NULL;
int id_Num ;
cout<<"Enter ID Number to search:"<<endl;
cin>>id_Num;
prev=head;
current=head;
while(current->idNum!=id_Num)
{
prev=current;
current=current->next;
}
cout<<" Name: ";
cout<<current->firstname;
cout<<current->lastname;
cout<<" ID number:";
cout<<current->idNum;
cout<<" GPA:";
cout<<current->gpa;
getch();
}
void Delete() //Deletes Student's Info//
{
node *ptr=NULL;
node *prev=NULL;
node *current=NULL;
int id_Num;
cout<<"Enter ID Number to Delete:"<<endl;
cin>>id_Num;
prev=head;
current=head;
while(current->idNum!=id_Num)
{
prev=current;
current=current->next;
}
prev->next = current->next;
current->next=NULL;
delete current;
cout<<endl<<"Student's Info is Deleted ";
getch();
}
int main()
{
int choice;
do
{
cout<<"Option: "<<endl;
cout<<"1. Add New Student"<<endl;
cout<<"2. Search for Student"<<endl;
cout<<"3. Modify a Student's Info"<<endl;
cout<<"4. Delete a Student's Info "<<endl;
cout<<"5. Terminate"<<endl;
cout<<"6. Display"<<endl;
cin>>choice;
cout<<choice<<endl;
if(choice==1)
{
Add();
}
else if(choice==2)
{
Search();
}
else if(choice==3)
{
Modify();
}
else if(choice==4)
{
Delete();
}
else if(choice==5)
{
exit(0);
}else if(choice==6){
cout<<"Hello"<<endl;
Display();
}
else
{
exit(0);
}
}while(choice!=5);
getch();
}
Explanation / Answer
If you have need any clarifications, please give me comment...
#include <iostream>
// #include <conio.h>
// #include <Windows.h>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
bool check = true;
struct node
{
char firstname[20];
char lastname[20];
int idNum;
float gpa;
node *next;
} * head, *lastptr;
void Display()
{
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
cout << "Linked list data is:" << endl;
node *temp = head;
while (temp != NULL)
{
cout << " *** ID NUMBER: " << temp->idNum << " *** NAME: " << temp->firstname << " " << temp->lastname << " *** GPA: " << temp->gpa << endl;
if (temp->next != NULL)
cout << "";
temp = temp->next;
}
}
void Add() //Add Student's Info//
{
node *p;
p = new node;
cout << "Enter name of student:" << endl;
cin >> p->firstname;
cin >> p->lastname;
fflush(stdin);
cout << "Enter Id Number of student:" << endl;
cin >> p->idNum;
fflush(stdin);
cout << "Enter GPA of student:" << endl;
cin >> p->gpa;
fflush(stdin);
p->next = NULL;
if (check)
{
head = p;
lastptr = p;
check = false;
}
else
{
lastptr->next = p;
lastptr = p;
}
cout << endl
<< "Student's Info is in the List ";
// getch();
}
void Modify() //Modifies Student's Info//
{
node *current = NULL;
int id_Num;
cout << "Enter Id Number to search:" << endl;
cin >> id_Num;
current = head;
while (current->idNum != id_Num)
{
current = current->next;
}
fflush(stdin);
cout << "Enter name of student:" << endl;
cin.getline(current->firstname, 20);
cin.getline(current->lastname, 20);
fflush(stdin);
cout << "Enter GPA of student:" << endl;
cin >> current->gpa;
fflush(stdin);
cout << endl
<< "Student's Info is Changed ";
// getch();
}
void Search() //Searches Student's Info//
{
node *prev = NULL;
node *current = NULL;
int id_Num;
cout << "Enter ID Number to search:" << endl;
cin >> id_Num;
prev = head;
current = head;
while (current->idNum != id_Num)
{
prev = current;
current = current->next;
}
cout << " Name: ";
cout << current->firstname;
cout << current->lastname;
cout << " ID number:";
cout << current->idNum;
cout << " GPA:";
cout << current->gpa;
// getch();
}
void Delete() //Deletes Student's Info//
{
node *ptr = NULL;
node *prev = NULL;
node *current = NULL;
int id_Num;
cout << "Enter ID Number to Delete:" << endl;
cin >> id_Num;
prev = head;
current = head;
if(head==NULL)
return;
while (current->idNum != id_Num)
{
prev = current;
current = current->next;
}
if(current==head)
head = current->next;
else if(current->next!=NULL)
prev->next = current->next;
else
prev->next = NULL;
delete current;
cout << endl
<< "Student's Info is Deleted ";
// getch();
}
int main()
{
int choice;
do
{
cout << "Option: "
<< endl;
cout << "1. Add New Student" << endl;
cout << "2. Search for Student" << endl;
cout << "3. Modify a Student's Info" << endl;
cout << "4. Delete a Student's Info " << endl;
cout << "5. Terminate" << endl;
cout << "6. Display" << endl;
cin >> choice;
cout << choice << endl;
if (choice == 1)
{
Add();
}
else if (choice == 2)
{
Search();
}
else if (choice == 3)
{
Modify();
}
else if (choice == 4)
{
Delete();
}
else if (choice == 5)
{
exit(0);
}
else if (choice == 6)
{
cout << "Hello" << endl;
Display();
}
else
{
exit(0);
}
} while (choice != 5);
// getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.