Need help! My C++ Program doesn't work when I ran the program especially in Adding, Searching and Deleting a record. What seems to be the error in the code? #include #include #include #include #include using namespace std; bool check = true; struct node //structure of node // { int rollNo; char name[20]; char address[40]; char gender[20]; char department[20]; int year; char dob[20]; node *next; }*head,*lastptr; void add() //Adds record of student// { node *p; p=new node; cout<<"Enter ID number of student:"<>p->rollNo; fflush(stdin); cout<<"Enter name of student:"<name); fflush(stdin); cout<<"Enter address of student:"<address); fflush(stdin); cout<<"Enter birthday of student:"<dob); fflush(stdin); cout<<"Enter gender of student:"<gender); fflush(stdin); cout<<"Enter department of student:"<department); fflush(stdin); cout<<"Enter Training year of student:"<>p->year; fflush(stdin); p->next=NULL; if(check) { head = p; lastptr = p; check = false; } else { lastptr->next=p; lastptr=p; } // Writing data to file ------------------------------------------------------- // Remove the '------' lines if not required ofstream file("student_data.txt"); file<<"------------------------------------------------------"<rollNo<name<address<dob<gender<department<year<>roll_no; prev=head; current=head; while(current->rollNo!=roll_no) { prev=current; current=current->next; } ptr=new node; fflush(stdin); cout<<"Enter ID number of student:"<>ptr->rollNo; fflush(stdin); cout<<"Enter name of student:"<name); fflush(stdin); cout<<"Enter address of student:"<address); fflush(stdin); cout<<"Enter birthday of student:"<dob); fflush(stdin); cout<<"Enter gender of student:"<gender); fflush(stdin); cout<<"Enter department of student:"<department); fflush(stdin); cout<<"Enter Training year of student:"<>ptr->year; fflush(stdin); prev->next=ptr; ptr->next=current->next; current->next=NULL; delete current; cout<>roll_no; prev=head; current=head; while(current->rollNo!=roll_no) { prev=current; current=current->next; } cout<<"\nID No:"; cout<rollNo; cout<<"\nname: "; puts(current->name); cout<<"\nAddress: "; puts(current->address); cout<<"\nGender: "; puts(current->gender); cout<<"\ndepartment: "; puts(current->department); cout<<"\nYear: "; cout<year; cout<<"\nDate of birth: "; puts(current->dob); } void del() { node *ptr=NULL; node *prev=NULL; node *current=NULL; int roll_no; cout<<"Enter ID Number to Delete:"<>roll_no; prev=head; current=head; while(current->rollNo!=roll_no) { prev=current; current=current->next; } prev->next = current->next; current->next=NULL; delete current; cout<Press '1' to add New record:"<Press '2' to search a record:"<Press '3' to modify a record:"<Press '4' to delete a record:"<Press '5' to exit:"<>x; switch(x){ case 1: system("cls"); add(); break; case 2: system("cls"); search(); break; case 3: system("cls"); modify(); break; case 4: system("cls"); del(); break; case 5: exit(0); break; default: cout<<"Enter a valid option. \n"; break; } }while(1); }
Need help! My C++ Program doesn't work when I ran the program especially in Adding, Searching and Deleting a record. What seems to be the error in the code?
#include<iostream>
#include<fstream>
#include<cctype>
#include<sstream>
#include<string>
using namespace std;
bool check = true;
struct node //structure of node //
{
int rollNo;
char name[20];
char address[40];
char gender[20];
char department[20];
int year;
char dob[20];
node *next;
}*head,*lastptr;
void add() //Adds record of student//
{
node *p;
p=new node;
cout<<"Enter ID number of student:"<<endl;
cin>>p->rollNo;
fflush(stdin);
cout<<"Enter name of student:"<<endl;
gets(p->name);
fflush(stdin);
cout<<"Enter address of student:"<<endl;
gets(p->address);
fflush(stdin);
cout<<"Enter birthday of student:"<<endl;
gets(p->dob);
fflush(stdin);
cout<<"Enter gender of student:"<<endl;
gets(p->gender);
fflush(stdin);
cout<<"Enter department of student:"<<endl;
gets(p->department);
fflush(stdin);
cout<<"Enter Training year of student:"<<endl;
cin>>p->year;
fflush(stdin);
p->next=NULL;
if(check)
{
head = p;
lastptr = p;
check = false;
}
else
{
lastptr->next=p;
lastptr=p;
}
// Writing data to file -------------------------------------------------------
// Remove the '------' lines if not required
ofstream file("student_data.txt");
file<<"------------------------------------------------------"<<endl;
file<<"Student Id: "<<lastptr->rollNo<<endl;
file<<"Student Name: "<<lastptr->name<<endl;
file<<"Student Address: "<<lastptr->address<<endl;
file<<"Student DOB: "<<lastptr->dob<<endl;
file<<"Student Gender: "<<lastptr->gender<<endl;
file<<"Student Department: "<<lastptr->department<<endl;
file<<"Student Training Year: "<<lastptr->year<<endl;
file<<"------------------------------------------------------"<<endl<<endl<<endl;
// ------------------------------
cout<<endl<<"Recored Entered"<<endl;
}
void modify() //modifies record of student//
{
node *ptr;
node *prev=NULL;
node *current=NULL;
int roll_no;
cout<<"Enter ID number to search:"<<endl;
cin>>roll_no;
prev=head;
current=head;
while(current->rollNo!=roll_no)
{
prev=current;
current=current->next;
}
ptr=new node;
fflush(stdin);
cout<<"Enter ID number of student:"<<endl;
cin>>ptr->rollNo;
fflush(stdin);
cout<<"Enter name of student:"<<endl;
gets(ptr->name);
fflush(stdin);
cout<<"Enter address of student:"<<endl;
gets(ptr->address);
fflush(stdin);
cout<<"Enter birthday of student:"<<endl;
gets(ptr->dob);
fflush(stdin);
cout<<"Enter gender of student:"<<endl;
gets(ptr->gender);
fflush(stdin);
cout<<"Enter department of student:"<<endl;
gets(ptr->department);
fflush(stdin);
cout<<"Enter Training year of student:"<<endl;
cin>>ptr->year;
fflush(stdin);
prev->next=ptr;
ptr->next=current->next;
current->next=NULL;
delete current;
cout<<endl<<"Recored Modified";
}
void search() //searches record of student//
{
node *prev=NULL;
node *current=NULL;
int roll_no;
cout<<"Enter ID Number to search:"<<endl;
cin>>roll_no;
prev=head;
current=head;
while(current->rollNo!=roll_no)
{
prev=current;
current=current->next;
}
cout<<"\nID No:";
cout<<current->rollNo;
cout<<"\nname: ";
puts(current->name);
cout<<"\nAddress: ";
puts(current->address);
cout<<"\nGender: ";
puts(current->gender);
cout<<"\ndepartment: ";
puts(current->department);
cout<<"\nYear: ";
cout<<current->year;
cout<<"\nDate of birth: ";
puts(current->dob);
}
void del()
{
node *ptr=NULL;
node *prev=NULL;
node *current=NULL;
int roll_no;
cout<<"Enter ID Number to Delete:"<<endl;
cin>>roll_no;
prev=head;
current=head;
while(current->rollNo!=roll_no)
{
prev=current;
current=current->next;
}
prev->next = current->next;
current->next=NULL;
delete current;
cout<<endl<<"Recored Deleted";
}
int main()
{
int x;
do
{
cout<<"1--->Press '1' to add New record:"<<endl;
cout<<"2--->Press '2' to search a record:"<<endl;
cout<<"3--->Press '3' to modify a record:"<<endl;
cout<<"4--->Press '4' to delete a record:"<<endl;
cout<<"5--->Press '5' to exit:"<<endl;
cin>>x;
switch(x){
case 1:
system("cls");
add();
break;
case 2:
system("cls");
search();
break;
case 3:
system("cls");
modify();
break;
case 4:
system("cls");
del();
break;
case 5:
exit(0);
break;
default:
cout<<"Enter a valid option. \n";
break;
}
}while(1);
}
Step by step
Solved in 4 steps with 1 images