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#includeusing 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="">"enter><> cin>>p->rollNo; fflush(stdin); cout<"enter name="" of="">"enter><> gets(p->name); fflush(stdin); cout<"enter address="" of="">"enter><> gets(p->address); fflush(stdin); cout<"enter birthday="" of="">"enter><> gets(p->dob); fflush(stdin); cout<"enter gender="" of="">"enter><> gets(p->gender); fflush(stdin); cout<"enter department="" of="">"enter><> gets(p->department); fflush(stdin); cout<"enter training="" year="" of="">"enter><> 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<><> file<"student id:="">"student>rollNo<> file<"student name:="">"student>name<> file<"student address:="">"student>address<> file<"student dob:="">"student>dob<> file<"student gender:="">"student>gender<> file<"student department:="">"student>department<> file<"student training="" year:="">"student>year<> file<><><><> // ------------------------------ cout<><"recored>"recored><>}void modify() //modifies record of student//{ node *ptr; node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> 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="">"enter><> cin>>ptr->rollNo; fflush(stdin); cout<"enter name="" of="">"enter><> gets(ptr->name); fflush(stdin); cout<"enter address="" of="">"enter><> gets(ptr->address); fflush(stdin); cout<"enter birthday="" of="">"enter><> gets(ptr->dob); fflush(stdin); cout<"enter gender="" of="">"enter><> gets(ptr->gender); fflush(stdin); cout<"enter department="" of="">"enter><> gets(ptr->department); fflush(stdin); cout<"enter training="" year="" of="">"enter><> cin>>ptr->year; fflush(stdin); prev->next=ptr; ptr->next=current->next; current->next=NULL; delete current; cout<><"recored>"recored>}void search() //searches record of student//{ node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> cin>>roll_no; prev=head; current=head; while(current->rollNo!=roll_no) { prev=current; current=current->next; } cout<"\nid>"\nid> coutrollNo; cout<"\nname:>"\nname:> puts(current->name); cout<"\naddress:>"\naddress:> puts(current->address); cout<"\ngender:>"\ngender:> puts(current->gender); cout<"\ndepartment:>"\ndepartment:> puts(current->department); cout<"\nyear:>"\nyear:> coutyear; cout<"\ndate of="" birth:="">"\ndate> puts(current->dob);}void del(){ node *ptr=NULL; node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> 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<><"recored>"recored>}int main(){ int x; do { cout<"1--->Press '1' to add New record:"<> cout<"2--->Press '2' to search a record:"<> cout<"3--->Press '3' to modify a record:"<> cout<"4--->Press '4' to delete a record:"<> cout<"5--->Press '5' to exit:"<> 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.="">"enter> break; } }while(1);}"5--->"4--->"3--->"2--->"1--->
void add() //Adds record of student//{ node *p; p=new node; cout<"enter id="" number="" of="">"enter><> cin>>p->rollNo; fflush(stdin); cout<"enter name="" of="">"enter><> gets(p->name); fflush(stdin); cout<"enter address="" of="">"enter><> gets(p->address); fflush(stdin); cout<"enter birthday="" of="">"enter><> gets(p->dob); fflush(stdin); cout<"enter gender="" of="">"enter><> gets(p->gender); fflush(stdin); cout<"enter department="" of="">"enter><> gets(p->department); fflush(stdin); cout<"enter training="" year="" of="">"enter><> 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<><> file<"student id:="">"student>rollNo<> file<"student name:="">"student>name<> file<"student address:="">"student>address<> file<"student dob:="">"student>dob<> file<"student gender:="">"student>gender<> file<"student department:="">"student>department<> file<"student training="" year:="">"student>year<> file<><><><> // ------------------------------
cout<><"recored>"recored><>}void modify() //modifies record of student//{ node *ptr; node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> 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="">"enter><> cin>>ptr->rollNo; fflush(stdin); cout<"enter name="" of="">"enter><> gets(ptr->name); fflush(stdin); cout<"enter address="" of="">"enter><> gets(ptr->address); fflush(stdin); cout<"enter birthday="" of="">"enter><> gets(ptr->dob); fflush(stdin); cout<"enter gender="" of="">"enter><> gets(ptr->gender); fflush(stdin); cout<"enter department="" of="">"enter><> gets(ptr->department); fflush(stdin); cout<"enter training="" year="" of="">"enter><> cin>>ptr->year; fflush(stdin); prev->next=ptr; ptr->next=current->next; current->next=NULL; delete current; cout<><"recored>"recored>}void search() //searches record of student//{ node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> cin>>roll_no; prev=head; current=head; while(current->rollNo!=roll_no) { prev=current; current=current->next; } cout<"\nid>"\nid> coutrollNo; cout<"\nname:>"\nname:> puts(current->name); cout<"\naddress:>"\naddress:> puts(current->address); cout<"\ngender:>"\ngender:> puts(current->gender); cout<"\ndepartment:>"\ndepartment:> puts(current->department); cout<"\nyear:>"\nyear:> coutyear; cout<"\ndate of="" birth:="">"\ndate> puts(current->dob);}void del(){ node *ptr=NULL; node *prev=NULL; node *current=NULL; int roll_no; cout<"enter id="" number="" to="">"enter><> 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<><"recored>"recored>}int main(){ int x; do { cout<"1--->Press '1' to add New record:"<> cout<"2--->Press '2' to search a record:"<> cout<"3--->Press '3' to modify a record:"<> cout<"4--->Press '4' to delete a record:"<> cout<"5--->Press '5' to exit:"<> 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.="">"enter> break; } }while(1);}"5--->"4--->"3--->"2--->"1--->
int main(){ int x;
do { cout<"1--->Press '1' to add New record:"<> cout<"2--->Press '2' to search a record:"<> cout<"3--->Press '3' to modify a record:"<> cout<"4--->Press '4' to delete a record:"<> cout<"5--->Press '5' to exit:"<> 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.="">"enter> break; } }while(1);}
"5--->"4--->"3--->"2--->"1--->
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here