Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***I need help editing and fixing this program, When you input the students name

ID: 3719587 • Letter: #

Question

***I need help editing and fixing this program, When you input the students name for example "John Doe" and then you ask to display the name it only displays the first name. Second when I try to modify the students info it does nothing. And last when i delete the student info and then i display it, it still shows the gpa and id number, i need that fixed as well to delete everything.***

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
bool check = true;
struct node
{
char name[20];
int idNum;
float gpa;

node *next;
}*head,*lastptr;
void Display(){
cout<<"linked list data is:"<<endl;
node *temp=head;
while(temp!=NULL)
{
cout<<temp->idNum<<" "<<temp->name<<" "<<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->name;
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->name);
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->name;
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

#include<iostream>
#include <ios>
#include <limits>
#include<fstream>
#include <string>

using namespace std;
bool check = true;

struct node
{
// string name;
char name[20];   
int idNum;
float gpa;

node *next;
}*head,*lastptr;
void Display(){
cout<<"linked list data is:"<<endl;
node *temp=head;
while(temp!=NULL)
{
cout<<temp->idNum<<" "<<temp->name<<" "<<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: ";
cin.getline(p->name, sizeof(p->name));
cout<<"Enter Id Number of student: ";
cin>>p->idNum;
cout<<"Enter GPA of student: ";
cin>>p->gpa;
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 ";
}

void Modify() //Modifies Student's Info//
{
node *prev=NULL;
node *current=NULL;
if (head == NULL) {
cout << "Empty list can't be modified by id num" << endl;
return;
}
int id_Num;
cout<<"Enter Id Number to search:"<<endl;
cin>>id_Num;
cin.ignore (std::numeric_limits<std::streamsize>::max(), ' ');
prev=head;
current=head;
while(current->idNum!=id_Num)
{
prev=current;
current=current->next;
}
if (current == NULL) {
cout << "ID not found"<< endl;
return;
}
cout<<"Enter name of student:"<<endl;
cin.getline(current->name, sizeof(current->name));
cout<<"Enter ID number of student:"<<endl;
cin>>current->idNum;
cout<<"Enter GPA of student:"<<endl;
cin>>current->gpa;
cout<<endl<<"Student's Info is Changed ";
}

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->name;
cout<<" ID number:";
cout<<current->idNum;
cout<<" GPA:";
cout<<current->gpa;
}
void Delete() //Deletes Student's Info//
{
node *ptr=NULL;
node *prev=NULL;
node *current=NULL;
if (head == NULL) {
cout << "Nothing to delete!" << endl;
return;
}
int id_Num;
cout<<"Enter ID Number to Delete:"<<endl;
cin>>id_Num;
prev=head;
current=head;
if (head->idNum == id_Num) {
head = head->next;
delete head;
return;
}
while(current->idNum!=id_Num)
{
prev=current;
current=current->next;
}
if (current == NULL) {
cout << "ID not found"<< endl;
return;
}
if (current->next == NULL) {
lastptr = prev;
}
prev->next = current->next;
current->next=NULL;
delete current;
cout<<endl<<"Student's Info is Deleted ";
}


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;
cin.ignore (std::numeric_limits<std::streamsize>::max(), ' ');
if(choice==1)
{
Add();
}
else if(choice==2)
{
Search();
}
else if(choice==3)
{
Modify();
}
else if(choice==4)
{
Delete();
}
else if(choice==5)
{
break;
}else if(choice==6){
cout<<"hello"<<endl;
Display();
}
// else
// {
// exit(0);
// }
//
}while(choice!=5);
}

Sample run

Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
1
Enter name of student: John Doe
Enter Id Number of student: 1
Enter GPA of student: 3.4

Student's Info is in the List

Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
1
Enter name of student: John Wick
Enter Id Number of student: 2
Enter GPA of student: 4.0

Student's Info is in the List

Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
6
hello
linked list data is:
1 John Doe 3.4
==>2 John Wick 4
Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
3
Enter Id Number to search:
2
Enter name of student:
John 2
Enter ID number of student:
2
Enter GPA of student:
3.6

Student's Info is Changed

Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
6
hello
linked list data is:
1 John Doe 3.4
==>2 John 2 3.6
Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
4
Enter ID Number to Delete:
1
Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display
6
hello
linked list data is:
2 3.6
Option:

1. Add New Student
2. Search for Student
3. Modify a Student's Info
4. Delete a Student's Info
5. Terminate
6. Display