PDA

View Full Version : دفترچه تلفن ساده در C++‎‎



IMAN4k
چهارشنبه 06 خرداد 1394, 11:54 صبح
دوستان در برنامه زیر mypb را به عنوان undefined ارور میده(در توابع edit و search و del) و دو گزینه break و continue وجود داره؟

// phonebook Win32 console application
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iomanip>
#include <fstream>
#include "conio.h"
using namespace std;

void menu ()
{
cout <<"1.add name\n2.delete number\n3.edit name\n4.edit number\n5.print\n6.search number\n7.search name\n8.clear\n9.exit\n";
}
class phonebook
{
private:
char *name,*number;
public:
void add ();//add one number(name+number) to the phone book
void del();//delete one number(name+number) from phone book
void edit_name ();//edit an existing name
void edit_number ();//edit an existing number
phonebook search_name ();//serach for specific name in phone book
phonebook search_number ();//search for specific number in phone book
void print ();//print the entire phone book
void clear ();//clear the entire phone book
};

void phonebook::add ()
{
phonebook mypb;
ofstream myfile("phonebook.txt", ios::app);
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
int t1,t2;
cout <<"your name size";
cin >>t1;
name=new char [t1];
cout <<"your name :";
cin >>mypb.name;
cout <<"your number size";
cin >>t2;
number=new char [t2];
cout <<"your number :";
cin >>mypb.number;
myfile <<mypb.name<<"\t"<<mypb.number<<"\n";
myfile.close ();
}
void phonebook::del ()
{
phonebook mypb;
char *id;
int t;
cout <<"your number size :";
cin >>t;
id=new char [t];
cout <<"your number :";
cin >>id;
ifstream myfile ("phonebook.txt");
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
myfile >>mypb.name>>mypb.number;
while (!myfile.eof())
{
if (! strcmp (id,mypb.number))
{
strcpy (mypb.name,"N/A");
strcpy (mypb.number,"N/A");
break ;
}
}
if(myfile.eof())
cout <<"this number isn't exist !";
myfile.close();
}

void phonebook::edit_name ()
{
phonebook mypb;
char *n;
int t;
cout <<"your name size";
cin >>t;
n=new char [t];
cout <<"your name :";
cin >>n;
ifstream myfile ("phonebook.txt");
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
myfile >>mypb.name>>mypb.number;
while (! myfile.eof())
{
if (!strcmp (n,mypb.name))
{
strcpy (mypb.name,n);
break ;
}
}
if(myfile.eof())
cout <<"can not find this name !";
myfile.close();
}
void phonebook::edit_number ()
{
phonebook mypb;
char *num;
int t;
cout <<"your number size";
cin >>t;
num=new char [t];
cout <<"your number :";
cin >>num;
ifstream myfile ("phonebook.txt");
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
myfile >>mypb.name>>mypb.number;
while (! myfile.eof())
{
if (!strcmp (mypb.number,num))
{
strcpy (mypb.number,num);
break ;
}
}
if(myfile.eof())
cout <<"can not find this number !";
myfile.close();
}

phonebook phonebook::search_name ()
{
phonebook mypb;
char *n;
int t;
cout <<"your name size";
cin >>t;
n=new char [t];
cout <<"your name :";
cin >>n;
ifstream myfile ("phonebook.txt");
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
myfile >>mypb.name>>mypb.number;
while (! myfile.eof())
{
if (!strcmp (mypb.name,n))
{
return mypb;
break ;
}
}
if(myfile.eof())
cout <<"this name isn't available !";
myfile.close();
}
phonebook phonebook::search_number ()
{
phonebook mypb;
char *num;
int t;
cout <<"your number size";
cin >>t;
num=new char [t];
cout <<"your number :";
cin >>num;
ifstream myfile ("phonebook.txt");
if(!myfile)
{
cerr<<"File can not open."<<endl;
exit(1);
}
myfile >>mypb.name>>mypb.number;
while (! myfile.eof())
{
if (!strcmp (num,mypb.number))
{
return mypb;
break ;
}
}
if(myfile.eof())
cout <<"this number isn't available !";
myfile.close();
}

void phonebook::print ()
{
ifstream myfile("phonebook.txt",ios::out);
while(!myfile.eof())
{
string output;
getline(myfile,output);
cout<<output<<"\n"<<endl;
}
}
void phonebook::clear ()
{
ofstream myfile ("phonebook.txt");
myfile.open("phonebook.txt",ofstream::out | ofstream::trunc);
myfile.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
phonebook i;
char answer;
while ( 1 )
{
menu();
cin >>answer;
if (answer == '9')
break ;
system("cls");
switch (answer)
{
case '1':i.add ();
break ;
case '2': i.del ();
break ;
case '3': i.edit_name ();
break ;
case '4': i.edit_number ();
break ;
case '5': i.print ();
break ;
case '6': i.search_number ();
break ;
case '7': i.search_name ();
break ;
case '8': i.clear ();
break ;
default :break ;

}
}
return 0;
}

IMAN4k
چهارشنبه 06 خرداد 1394, 16:38 عصر
دوستان کمک کنید!

ali chegini
پنج شنبه 07 خرداد 1394, 16:16 عصر
سلام.
اینو بعد از اینکلود ها اضافه کن.

class phonebook;