PhoneBook Application using C++ File Handling [on hold]
up vote
-4
down vote
favorite
I made a phone book application using C++ and adding file handling as an Assignment.
I am having certain problems as I have yet not studied file handling in detail and is noob when it comes to programming. I am having problem here with file handling. The code is incomplete for group creation, however it's not even working properly for writing and reading into file. I don't understand what mistakes did I do.
I understand that the code is made complex. I would Like you to help me how can I make it work.
I am sorry if asking something like this goes against the rules of this community.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//system("cls");
using namespace std;
int checkName(string const &x);
int checkPhno(string const &x);
int checkEmail(string const &x);
class phoneBook/*::private FileHandler*/{
string name;
string phno;
string email;
public:
void edit();
void putdata();
void showdata();
void update();
void changeRecord(phoneBook const &x);
void write();
void readall();
void data();
void del(string const n);
void search();
string getname(){
return name;
}
string getphno(){
return phno;
}
string getemail(){
return email;
}
};
void phoneBook :: putdata(){
cout<<"nEnter Name : ";
cin.ignore();
getline(cin,name);
cout<<"nEnter Phone Number : ";
cin.ignore();
getline(cin,phno);
cout<<"nEnter Email : ";
cin.ignore();
getline(cin,email);
while(!checkName(name)){
cout<<"Name Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,name);
if(name[0]==27)
return;
}
while(!checkPhno(phno)){
cout<<"Phone No. Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,phno);
if(phno[0]==27)
return;
}
while(!checkEmail(email)){
cout<<"Email Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
getline(cin,email);
cin.ignore();
if(name[0]==27)
return;
}
write();
}
void phoneBook :: data(){
//system("cls");
cout<<"Name: "<<name<<endl;
cout<<"Phone No. +91 "<<phno<<endl;
cout<<"Email: "<<email<<endl;
}
void phoneBook :: showdata(){
data();
cout<<"1. Editn2. Deleten3. Returnn";
int c;
cin>>c;
switch(c){
case 1:
update();
break;
case 2:
del(getname());
break;
case 3:
return;
default:
cout<<"Wrong Choice!!! Returning Back!!!!!n";
}
}
void phoneBook :: update(){
int ch=0;
phoneBook temp;
cout<<"What would You like to update n";
cout<<"1. Namen";
cout<<"2. Phone no.n";
cout<<"3. Email Addressn";
cout<<"4. Donen";
do{
cin>>ch;
switch(ch){
case 1:
cin.ignore();
getline(cin,temp.name);
//name=temp;
break;
case 2:
cin.ignore();
getline(cin,temp.phno);
//phno=temp;
break;
case 3:
cin.ignore();
getline(cin,temp.email);
//email=temp;
break;
case 4:
break;
default:
cout<<"Wrong choice! Enter again: ";
break;
}
cout<<"Enter the choice: ";
}while(ch!=4);
changeRecord(temp);
}
void phoneBook :: changeRecord(phoneBook const &x){
phoneBook tmp;
fstream file("PhoneBook.txt", ios::in|ios::out|ios::ate);
file.seekg(0);
do{
file.read((char *)&tmp,sizeof(tmp));
if((this->name).compare(tmp.name)){
// file.seekp(file.tellp()-sizeof(tmp));
file.seekp(-sizeof(tmp),ios::cur);
file.write((char*)&x,sizeof(x));
}
}while(!file.eof());
}
void phoneBook :: search(){
}
void phoneBook :: write(){
ofstream file_w;
file_w.open("PhoneBook.txt", ios::app);
file_w.write((char*)this, sizeof(*this)); //this signifies that the object which called this function
file_w.close();
cout<<"Contact Insertedn";
}
void phoneBook :: readall(){
phoneBook x;
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
if(!file_r){
cout<<"File Not Foundn";
return;
}
do{
file_r.read((char*)&x, sizeof(x));
x.data();
}while(file_r.eof());
file_r.close();
}
void phoneBook :: del(string const n){
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
ofstream file_w;
file_w.open("temp.txt",ios::out);
if(!file_r){
cout<<"File Not Foundn";
return;
}
while(!file_r.eof()){
file_r.read((char*)this, sizeof(*this));
if( !n.compare(name) ){
file_w.write((char*)this, sizeof(*this));
}
}
file_r.close();
file_w.close();
remove("PhoneBook.txt");
rename("temp.txt","PhoneBook.txt");
}
void phoneBook :: edit(){
search();
update();
cout<<"Updated Contacts: n";
showdata();
//delay(500);
}
int checkName(string const &x){
return 1;
}
int checkPhno(string const &x){
return 1;
}
int checkEmail(string const &x){
return 1;
}
int main(){
phoneBook p;
enum {
Show = 1,
Add,
Edit,
Search,
Remove,
Create,
View,
Exit
};
string choice;
int ch;
bool Continue=true;
while(Continue){
//system("cls");
cout << "nWhat would you like to do?: " << "n";
cout << "1. Show All Contacts" << "n";
cout << "2. Add A Contact" << "n";
cout << "3. Edit A Contact" << "n";
cout << "4. Search Contacts" << "n";//
cout << "5. Remove A Contact" << "n";
cout << "6. Create Groups" << "n";//
cout << "7. View Groups" << "n";//
cout << "8. Exit The Program" << "nn";
//getline(cin,choice);
cin>>choice;
try{
ch=stoi(choice);
}
catch(...){
ch=0;
}
switch(ch){
case Show:
p.readall();
break;
case Add:
p.putdata();
break;
case Edit:
p.update();
break;
case Search:
p.search();
break;
case Remove:
p.search();
p.del("abc");
break;
case Create:
break;
case View:
break;
case Exit:
Continue=false;
break;
}
}
cout<<"Exiting!!!!!n";
return 0;
}
The application is a console menu driven program to
- show all contacts Inserted
- Enter New Contact
- Edit contact
- Search contact
- Remove Contact
- some other stuffs
- exit
The problem arises in writing and reading the file in functions
- write(); doesn't write .txt in readable format
- readall(); doesn't display the data correctly
and some other issues which I think I can solve by myself if I can solve the above problems.
c++ error-handling
New contributor
put on hold as off-topic by πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta Nov 18 at 20:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-4
down vote
favorite
I made a phone book application using C++ and adding file handling as an Assignment.
I am having certain problems as I have yet not studied file handling in detail and is noob when it comes to programming. I am having problem here with file handling. The code is incomplete for group creation, however it's not even working properly for writing and reading into file. I don't understand what mistakes did I do.
I understand that the code is made complex. I would Like you to help me how can I make it work.
I am sorry if asking something like this goes against the rules of this community.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//system("cls");
using namespace std;
int checkName(string const &x);
int checkPhno(string const &x);
int checkEmail(string const &x);
class phoneBook/*::private FileHandler*/{
string name;
string phno;
string email;
public:
void edit();
void putdata();
void showdata();
void update();
void changeRecord(phoneBook const &x);
void write();
void readall();
void data();
void del(string const n);
void search();
string getname(){
return name;
}
string getphno(){
return phno;
}
string getemail(){
return email;
}
};
void phoneBook :: putdata(){
cout<<"nEnter Name : ";
cin.ignore();
getline(cin,name);
cout<<"nEnter Phone Number : ";
cin.ignore();
getline(cin,phno);
cout<<"nEnter Email : ";
cin.ignore();
getline(cin,email);
while(!checkName(name)){
cout<<"Name Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,name);
if(name[0]==27)
return;
}
while(!checkPhno(phno)){
cout<<"Phone No. Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,phno);
if(phno[0]==27)
return;
}
while(!checkEmail(email)){
cout<<"Email Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
getline(cin,email);
cin.ignore();
if(name[0]==27)
return;
}
write();
}
void phoneBook :: data(){
//system("cls");
cout<<"Name: "<<name<<endl;
cout<<"Phone No. +91 "<<phno<<endl;
cout<<"Email: "<<email<<endl;
}
void phoneBook :: showdata(){
data();
cout<<"1. Editn2. Deleten3. Returnn";
int c;
cin>>c;
switch(c){
case 1:
update();
break;
case 2:
del(getname());
break;
case 3:
return;
default:
cout<<"Wrong Choice!!! Returning Back!!!!!n";
}
}
void phoneBook :: update(){
int ch=0;
phoneBook temp;
cout<<"What would You like to update n";
cout<<"1. Namen";
cout<<"2. Phone no.n";
cout<<"3. Email Addressn";
cout<<"4. Donen";
do{
cin>>ch;
switch(ch){
case 1:
cin.ignore();
getline(cin,temp.name);
//name=temp;
break;
case 2:
cin.ignore();
getline(cin,temp.phno);
//phno=temp;
break;
case 3:
cin.ignore();
getline(cin,temp.email);
//email=temp;
break;
case 4:
break;
default:
cout<<"Wrong choice! Enter again: ";
break;
}
cout<<"Enter the choice: ";
}while(ch!=4);
changeRecord(temp);
}
void phoneBook :: changeRecord(phoneBook const &x){
phoneBook tmp;
fstream file("PhoneBook.txt", ios::in|ios::out|ios::ate);
file.seekg(0);
do{
file.read((char *)&tmp,sizeof(tmp));
if((this->name).compare(tmp.name)){
// file.seekp(file.tellp()-sizeof(tmp));
file.seekp(-sizeof(tmp),ios::cur);
file.write((char*)&x,sizeof(x));
}
}while(!file.eof());
}
void phoneBook :: search(){
}
void phoneBook :: write(){
ofstream file_w;
file_w.open("PhoneBook.txt", ios::app);
file_w.write((char*)this, sizeof(*this)); //this signifies that the object which called this function
file_w.close();
cout<<"Contact Insertedn";
}
void phoneBook :: readall(){
phoneBook x;
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
if(!file_r){
cout<<"File Not Foundn";
return;
}
do{
file_r.read((char*)&x, sizeof(x));
x.data();
}while(file_r.eof());
file_r.close();
}
void phoneBook :: del(string const n){
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
ofstream file_w;
file_w.open("temp.txt",ios::out);
if(!file_r){
cout<<"File Not Foundn";
return;
}
while(!file_r.eof()){
file_r.read((char*)this, sizeof(*this));
if( !n.compare(name) ){
file_w.write((char*)this, sizeof(*this));
}
}
file_r.close();
file_w.close();
remove("PhoneBook.txt");
rename("temp.txt","PhoneBook.txt");
}
void phoneBook :: edit(){
search();
update();
cout<<"Updated Contacts: n";
showdata();
//delay(500);
}
int checkName(string const &x){
return 1;
}
int checkPhno(string const &x){
return 1;
}
int checkEmail(string const &x){
return 1;
}
int main(){
phoneBook p;
enum {
Show = 1,
Add,
Edit,
Search,
Remove,
Create,
View,
Exit
};
string choice;
int ch;
bool Continue=true;
while(Continue){
//system("cls");
cout << "nWhat would you like to do?: " << "n";
cout << "1. Show All Contacts" << "n";
cout << "2. Add A Contact" << "n";
cout << "3. Edit A Contact" << "n";
cout << "4. Search Contacts" << "n";//
cout << "5. Remove A Contact" << "n";
cout << "6. Create Groups" << "n";//
cout << "7. View Groups" << "n";//
cout << "8. Exit The Program" << "nn";
//getline(cin,choice);
cin>>choice;
try{
ch=stoi(choice);
}
catch(...){
ch=0;
}
switch(ch){
case Show:
p.readall();
break;
case Add:
p.putdata();
break;
case Edit:
p.update();
break;
case Search:
p.search();
break;
case Remove:
p.search();
p.del("abc");
break;
case Create:
break;
case View:
break;
case Exit:
Continue=false;
break;
}
}
cout<<"Exiting!!!!!n";
return 0;
}
The application is a console menu driven program to
- show all contacts Inserted
- Enter New Contact
- Edit contact
- Search contact
- Remove Contact
- some other stuffs
- exit
The problem arises in writing and reading the file in functions
- write(); doesn't write .txt in readable format
- readall(); doesn't display the data correctly
and some other issues which I think I can solve by myself if I can solve the above problems.
c++ error-handling
New contributor
put on hold as off-topic by πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta Nov 18 at 20:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta
If this question can be reworded to fit the rules in the help center, please edit the question.
4
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I made a phone book application using C++ and adding file handling as an Assignment.
I am having certain problems as I have yet not studied file handling in detail and is noob when it comes to programming. I am having problem here with file handling. The code is incomplete for group creation, however it's not even working properly for writing and reading into file. I don't understand what mistakes did I do.
I understand that the code is made complex. I would Like you to help me how can I make it work.
I am sorry if asking something like this goes against the rules of this community.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//system("cls");
using namespace std;
int checkName(string const &x);
int checkPhno(string const &x);
int checkEmail(string const &x);
class phoneBook/*::private FileHandler*/{
string name;
string phno;
string email;
public:
void edit();
void putdata();
void showdata();
void update();
void changeRecord(phoneBook const &x);
void write();
void readall();
void data();
void del(string const n);
void search();
string getname(){
return name;
}
string getphno(){
return phno;
}
string getemail(){
return email;
}
};
void phoneBook :: putdata(){
cout<<"nEnter Name : ";
cin.ignore();
getline(cin,name);
cout<<"nEnter Phone Number : ";
cin.ignore();
getline(cin,phno);
cout<<"nEnter Email : ";
cin.ignore();
getline(cin,email);
while(!checkName(name)){
cout<<"Name Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,name);
if(name[0]==27)
return;
}
while(!checkPhno(phno)){
cout<<"Phone No. Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,phno);
if(phno[0]==27)
return;
}
while(!checkEmail(email)){
cout<<"Email Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
getline(cin,email);
cin.ignore();
if(name[0]==27)
return;
}
write();
}
void phoneBook :: data(){
//system("cls");
cout<<"Name: "<<name<<endl;
cout<<"Phone No. +91 "<<phno<<endl;
cout<<"Email: "<<email<<endl;
}
void phoneBook :: showdata(){
data();
cout<<"1. Editn2. Deleten3. Returnn";
int c;
cin>>c;
switch(c){
case 1:
update();
break;
case 2:
del(getname());
break;
case 3:
return;
default:
cout<<"Wrong Choice!!! Returning Back!!!!!n";
}
}
void phoneBook :: update(){
int ch=0;
phoneBook temp;
cout<<"What would You like to update n";
cout<<"1. Namen";
cout<<"2. Phone no.n";
cout<<"3. Email Addressn";
cout<<"4. Donen";
do{
cin>>ch;
switch(ch){
case 1:
cin.ignore();
getline(cin,temp.name);
//name=temp;
break;
case 2:
cin.ignore();
getline(cin,temp.phno);
//phno=temp;
break;
case 3:
cin.ignore();
getline(cin,temp.email);
//email=temp;
break;
case 4:
break;
default:
cout<<"Wrong choice! Enter again: ";
break;
}
cout<<"Enter the choice: ";
}while(ch!=4);
changeRecord(temp);
}
void phoneBook :: changeRecord(phoneBook const &x){
phoneBook tmp;
fstream file("PhoneBook.txt", ios::in|ios::out|ios::ate);
file.seekg(0);
do{
file.read((char *)&tmp,sizeof(tmp));
if((this->name).compare(tmp.name)){
// file.seekp(file.tellp()-sizeof(tmp));
file.seekp(-sizeof(tmp),ios::cur);
file.write((char*)&x,sizeof(x));
}
}while(!file.eof());
}
void phoneBook :: search(){
}
void phoneBook :: write(){
ofstream file_w;
file_w.open("PhoneBook.txt", ios::app);
file_w.write((char*)this, sizeof(*this)); //this signifies that the object which called this function
file_w.close();
cout<<"Contact Insertedn";
}
void phoneBook :: readall(){
phoneBook x;
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
if(!file_r){
cout<<"File Not Foundn";
return;
}
do{
file_r.read((char*)&x, sizeof(x));
x.data();
}while(file_r.eof());
file_r.close();
}
void phoneBook :: del(string const n){
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
ofstream file_w;
file_w.open("temp.txt",ios::out);
if(!file_r){
cout<<"File Not Foundn";
return;
}
while(!file_r.eof()){
file_r.read((char*)this, sizeof(*this));
if( !n.compare(name) ){
file_w.write((char*)this, sizeof(*this));
}
}
file_r.close();
file_w.close();
remove("PhoneBook.txt");
rename("temp.txt","PhoneBook.txt");
}
void phoneBook :: edit(){
search();
update();
cout<<"Updated Contacts: n";
showdata();
//delay(500);
}
int checkName(string const &x){
return 1;
}
int checkPhno(string const &x){
return 1;
}
int checkEmail(string const &x){
return 1;
}
int main(){
phoneBook p;
enum {
Show = 1,
Add,
Edit,
Search,
Remove,
Create,
View,
Exit
};
string choice;
int ch;
bool Continue=true;
while(Continue){
//system("cls");
cout << "nWhat would you like to do?: " << "n";
cout << "1. Show All Contacts" << "n";
cout << "2. Add A Contact" << "n";
cout << "3. Edit A Contact" << "n";
cout << "4. Search Contacts" << "n";//
cout << "5. Remove A Contact" << "n";
cout << "6. Create Groups" << "n";//
cout << "7. View Groups" << "n";//
cout << "8. Exit The Program" << "nn";
//getline(cin,choice);
cin>>choice;
try{
ch=stoi(choice);
}
catch(...){
ch=0;
}
switch(ch){
case Show:
p.readall();
break;
case Add:
p.putdata();
break;
case Edit:
p.update();
break;
case Search:
p.search();
break;
case Remove:
p.search();
p.del("abc");
break;
case Create:
break;
case View:
break;
case Exit:
Continue=false;
break;
}
}
cout<<"Exiting!!!!!n";
return 0;
}
The application is a console menu driven program to
- show all contacts Inserted
- Enter New Contact
- Edit contact
- Search contact
- Remove Contact
- some other stuffs
- exit
The problem arises in writing and reading the file in functions
- write(); doesn't write .txt in readable format
- readall(); doesn't display the data correctly
and some other issues which I think I can solve by myself if I can solve the above problems.
c++ error-handling
New contributor
I made a phone book application using C++ and adding file handling as an Assignment.
I am having certain problems as I have yet not studied file handling in detail and is noob when it comes to programming. I am having problem here with file handling. The code is incomplete for group creation, however it's not even working properly for writing and reading into file. I don't understand what mistakes did I do.
I understand that the code is made complex. I would Like you to help me how can I make it work.
I am sorry if asking something like this goes against the rules of this community.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//system("cls");
using namespace std;
int checkName(string const &x);
int checkPhno(string const &x);
int checkEmail(string const &x);
class phoneBook/*::private FileHandler*/{
string name;
string phno;
string email;
public:
void edit();
void putdata();
void showdata();
void update();
void changeRecord(phoneBook const &x);
void write();
void readall();
void data();
void del(string const n);
void search();
string getname(){
return name;
}
string getphno(){
return phno;
}
string getemail(){
return email;
}
};
void phoneBook :: putdata(){
cout<<"nEnter Name : ";
cin.ignore();
getline(cin,name);
cout<<"nEnter Phone Number : ";
cin.ignore();
getline(cin,phno);
cout<<"nEnter Email : ";
cin.ignore();
getline(cin,email);
while(!checkName(name)){
cout<<"Name Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,name);
if(name[0]==27)
return;
}
while(!checkPhno(phno)){
cout<<"Phone No. Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
cin.ignore();
getline(cin,phno);
if(phno[0]==27)
return;
}
while(!checkEmail(email)){
cout<<"Email Already Exisits!!!!!n";
cout<<"Enter again. Or press Esc and then Enter to Cancel. n";
getline(cin,email);
cin.ignore();
if(name[0]==27)
return;
}
write();
}
void phoneBook :: data(){
//system("cls");
cout<<"Name: "<<name<<endl;
cout<<"Phone No. +91 "<<phno<<endl;
cout<<"Email: "<<email<<endl;
}
void phoneBook :: showdata(){
data();
cout<<"1. Editn2. Deleten3. Returnn";
int c;
cin>>c;
switch(c){
case 1:
update();
break;
case 2:
del(getname());
break;
case 3:
return;
default:
cout<<"Wrong Choice!!! Returning Back!!!!!n";
}
}
void phoneBook :: update(){
int ch=0;
phoneBook temp;
cout<<"What would You like to update n";
cout<<"1. Namen";
cout<<"2. Phone no.n";
cout<<"3. Email Addressn";
cout<<"4. Donen";
do{
cin>>ch;
switch(ch){
case 1:
cin.ignore();
getline(cin,temp.name);
//name=temp;
break;
case 2:
cin.ignore();
getline(cin,temp.phno);
//phno=temp;
break;
case 3:
cin.ignore();
getline(cin,temp.email);
//email=temp;
break;
case 4:
break;
default:
cout<<"Wrong choice! Enter again: ";
break;
}
cout<<"Enter the choice: ";
}while(ch!=4);
changeRecord(temp);
}
void phoneBook :: changeRecord(phoneBook const &x){
phoneBook tmp;
fstream file("PhoneBook.txt", ios::in|ios::out|ios::ate);
file.seekg(0);
do{
file.read((char *)&tmp,sizeof(tmp));
if((this->name).compare(tmp.name)){
// file.seekp(file.tellp()-sizeof(tmp));
file.seekp(-sizeof(tmp),ios::cur);
file.write((char*)&x,sizeof(x));
}
}while(!file.eof());
}
void phoneBook :: search(){
}
void phoneBook :: write(){
ofstream file_w;
file_w.open("PhoneBook.txt", ios::app);
file_w.write((char*)this, sizeof(*this)); //this signifies that the object which called this function
file_w.close();
cout<<"Contact Insertedn";
}
void phoneBook :: readall(){
phoneBook x;
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
if(!file_r){
cout<<"File Not Foundn";
return;
}
do{
file_r.read((char*)&x, sizeof(x));
x.data();
}while(file_r.eof());
file_r.close();
}
void phoneBook :: del(string const n){
ifstream file_r;
file_r.open("PhoneBook.txt", ios::in);
ofstream file_w;
file_w.open("temp.txt",ios::out);
if(!file_r){
cout<<"File Not Foundn";
return;
}
while(!file_r.eof()){
file_r.read((char*)this, sizeof(*this));
if( !n.compare(name) ){
file_w.write((char*)this, sizeof(*this));
}
}
file_r.close();
file_w.close();
remove("PhoneBook.txt");
rename("temp.txt","PhoneBook.txt");
}
void phoneBook :: edit(){
search();
update();
cout<<"Updated Contacts: n";
showdata();
//delay(500);
}
int checkName(string const &x){
return 1;
}
int checkPhno(string const &x){
return 1;
}
int checkEmail(string const &x){
return 1;
}
int main(){
phoneBook p;
enum {
Show = 1,
Add,
Edit,
Search,
Remove,
Create,
View,
Exit
};
string choice;
int ch;
bool Continue=true;
while(Continue){
//system("cls");
cout << "nWhat would you like to do?: " << "n";
cout << "1. Show All Contacts" << "n";
cout << "2. Add A Contact" << "n";
cout << "3. Edit A Contact" << "n";
cout << "4. Search Contacts" << "n";//
cout << "5. Remove A Contact" << "n";
cout << "6. Create Groups" << "n";//
cout << "7. View Groups" << "n";//
cout << "8. Exit The Program" << "nn";
//getline(cin,choice);
cin>>choice;
try{
ch=stoi(choice);
}
catch(...){
ch=0;
}
switch(ch){
case Show:
p.readall();
break;
case Add:
p.putdata();
break;
case Edit:
p.update();
break;
case Search:
p.search();
break;
case Remove:
p.search();
p.del("abc");
break;
case Create:
break;
case View:
break;
case Exit:
Continue=false;
break;
}
}
cout<<"Exiting!!!!!n";
return 0;
}
The application is a console menu driven program to
- show all contacts Inserted
- Enter New Contact
- Edit contact
- Search contact
- Remove Contact
- some other stuffs
- exit
The problem arises in writing and reading the file in functions
- write(); doesn't write .txt in readable format
- readall(); doesn't display the data correctly
and some other issues which I think I can solve by myself if I can solve the above problems.
c++ error-handling
c++ error-handling
New contributor
New contributor
edited Nov 18 at 10:44
New contributor
asked Nov 18 at 10:19
Vimal Kumawat
11
11
New contributor
New contributor
put on hold as off-topic by πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta Nov 18 at 20:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta Nov 18 at 20:28
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, t3chb0t, 200_success, Snowhawk, Zeta
If this question can be reworded to fit the rules in the help center, please edit the question.
4
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35
add a comment |
4
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35
4
4
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
4
CodeReview is for improving code that works correctly, to the best of the user's knowledge. For code that doesn't work, it's best to make a minimal example and post a specific question (what do you expect the code to do? what actually happens?) on StackOverflow.
– user673679
Nov 18 at 10:35