Hello every one
am in real trouble now. i am in my 1st and last sem of my programing course. i need to write a program that can add delete display and update record. i got this program so far but the real prototype works fine for this program and after i edit it. it makes problem like after i input data it goes mad. and if i put wrong character it also goes mad ( like if in number i put alphabet )........
i don have any idea how to fix this but i think the problem is in
( line 190- line 196)
( line 209- line 212)
(line 80)
(line 84)
this is the code that needed to be fix
#include<iostream>
#include<fstream>
#include<string>
#include <stdlib.h>
using namespace std;
struct emp{
int eno,date,published;
string name,author,category,publisher;
};
emp e;
void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen
int main(){
string dummy;
int choice;
cout<<"===== Main Menu ====="<<endl;
cout<<endl<<endl;
cout<<"1 -> Add Record "<<endl;
cout<<"2 -> Display Record"<<endl;
cout<<"3 -> Delete Record"<<endl;
cout<<"4 -> Update Record"<<endl;
cout<<endl;
cout<<"0 -> Exit"<<endl;
cout<<endl<<endl<<"Selection ";
cin>>choice;
if(choice<0 || choice>4){
cls();
main();
}
switch(choice){
case 0: break;
case 1: addEmp(); break;
case 2: disEmp(); break;
case 3: delEmp(); break;
case 4: updEmp(); break;
}
}
void addEmp(){
fstream data;
string dummy;
emp e;
int choice;
do{
cls();
cout<<"===== Enter New Record ====="<<endl;
cout<<endl<<endl;
cout<<"Eno :\t\t"; cin>>e.eno;
getline(cin,dummy); //just to clear input buffer
cout<<"\nBook Name :\t\t";
cin.get();
getline (cin,e.name);
cout<<"\nBook Author :\t";
cin.get();
getline (cin,e.author);
cout<<"\nPublisher :\t";
cin.get();
getline (cin,e.publisher);
cout<<"\nCategory :\t";
cin.get();
getline (cin,e.category);
cout<<"\nPublished :\t";
cin.get();
cin >> e.published;
cout<<"\nDate added :\t";
cin.get();
cin>> e.date;
cout<<endl<<endl;
cout<<"1 -> Save | 2 -> Cancel"<<endl;
cin>>choice;
}
while(!(choice==1 || choice==2));
if(choice==1){
data.open("data.dat",ios::out|ios::app);
data.write((char*)&e,sizeof(e));
data.close();
cout<<"\nRecord Saved "<<endl<<endl;
}else{
cout<<"\n\nRecord Cancelled"<<endl<<endl;
}
cout<<"Enter another record?"<<endl;
cout<<"1 -> Yes | 2 -> No"<<endl;
cin>>choice;
if(choice==1)
addEmp();
else
return;
main();
}
void disEmp(){
fstream data;
emp e;
data.open("data.dat",ios::in);
cls();
while(data.read((char*) &e, sizeof(e))){
cout<<"\n\n-----Displaying record for ENO " <<e.eno<<endl;
cout<<"\nBook Name :\t\t"<<e.name;
cout<<"\nBook Author :\t"<<e.author;
cout<<"\nPublisher :\t"<<e.publisher;
cout<<"\nCategory :\t"<<e.category;
cout<<"\nPublished :\t"<<e.published;
cout<<"\nDate added :\t"<<e.date<<endl;
cout<<endl;
}
data.close();
main();
}
void delEmp(){
fstream data, temp;
emp e;
int eno;
cls();
cout<<"===== Delete Record ====="<<endl;
cout<<endl<<endl<<endl;
cout<<"Enter ENO to delete record ";cin>>eno;
rename("data.dat","temp.dat");
data.open("data.dat",ios::app);
temp.open("temp.dat",ios::in);
temp.seekg(0,ios::beg);
while(temp.read((char*)&e,sizeof(e))){
if(!(e.eno==eno)){
cout<<"Record to delete "<<e.eno;
data.write((char*) &e,sizeof(e));
}
}
data.close();
temp.close();
system("Del temp.dat");
main();
}
void updEmp(){
fstream data, temp;
emp e;
string dummy;
int eno;
cls();
cout<<"===== Update Record ====="<<endl;
cout<<endl<<endl<<endl;
cout<<"Enter ENO :_";cin>>eno;
rename("data.dat","temp.dat");
data.open("data.dat",ios::app);
temp.open("temp.dat",ios::in);
temp.seekg(0,ios::beg);
while(temp.read((char*)&e,sizeof(e))){
if(e.eno==eno){
cout<<"-----Current data for ENO: "<<e.eno;
cout<<"\nBook Name :\t\t"<<e.name;
cout<<"\nBook Author :\t"<<e.author;
cout<<"\nPublisher :\t"<<e.publisher;
cout<<"\nCategory :\t"<<e.category;
cout<<"\nPublished :\t"<<e.published;
cout<<"\nDate added :\t"<<e.date<<endl;
cout<<endl<<endl;
cout<<"-----Enter New Values-----"<<endl;
cout<<"\nEno :\t\t"; cin>>e.eno;
// getline(cin,dummy); //just to clear input buffer
cout<<"\nBook Name :\t\t"<<e.name;
cout<<"\nBook Author :\t"<<e.author;
cout<<"\nPublisher :\t" <<e.publisher;
cout<<"\nCategory :\t" <<e.category;
cout<<"\nPublished :\t"<<e.published;
cout<<"\nDate Added :\t" <<e.date;
cout<<endl<<endl;
}
data.write((char*) &e,sizeof(e));
}
data.close();
temp.close();
system("Del temp.dat");
main();
}
void cls(){
for (int i=0;i<50;i++)
cout<<"\n";
}