PDA

View Full Version : ** ایراد کار با فایل این چیه؟؟**



tohid.t
چهارشنبه 04 بهمن 1391, 21:37 عصر
سلام بر مهندسین گرامی!
این یک برنامه ایجاد پروفایل است.البته نباید پروفایل با آی دی تکراری ایجاد شود(سیو شود)
اگر قسمتی که چک میکند که پروفایل تکراری نباشه رو کامنت کنم درست سیو میشه ولی اگه کامنت نشه درست چک میکنه ولی هیچ وقت سیو نمی کنه!
لطفا برنامه رو با حداقل تغییرات و توضیحات کامل عیب یابی بفرمایید. و اشکال کارم رو توضیح بدید! لطفا راهنماییم کنید.فقط دو روز فرصت دارم!!
این شما برنامه نویسان و این برنامه!
باتشکر!
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
class profile
{
int _id;
char fname[20];
public:
int create_profile()
{
cout<<"\nEnter The id : ";
cin>>_id;
cout<<"\nEnter ur Name : ";
cin>> fname;
return _id;
}
int ret_id()
{
return _id;
}
void show_profile()
{
cout<<"\n profile No. : "<<_id;
cout<<"\n profile Holder Name : "<<fname;
}
}; // end of class
class my_database
{
fstream my_db;
profile me;
public:
int write()
{
char flag=true;
my_db.open("my_file.dat" , ios::in | ios::out | ios::app);
// Verify that the file has been successfully opened.
if(!my_db)
{
cout << "Cannot open file.\n";
_getch();
return 1;
}
int id = me.create_profile();
//*in gesmat ru comment koni write
while(my_db.read((char*)&me,sizeof(profile)))
{
if(me.ret_id()==id)
{
flag=false;
cout<<"\n\n\nYour creat profile failed because of same id error ...";
}
}
//*/
if(flag==true)
{
my_db.write((char*)&me,sizeof(profile));
cout<<"\n\n\nYour profile Created Successfully ...";
}
// Explicitly close the file.
my_db.close();
if(my_db.is_open())
{
cout << "A file close error occurred.";
}
return 1;
}
void display()
{
int n;
cout<<"\n\nEnter the profile id : ";
cin>>n;
char flag=0;
my_db.open("my_file.dat",ios::in);
if(!my_db)
{
cout << "Cannot open file.\n";
}
while(my_db.read((char*)&me,sizeof(profile)))
{
if(me.ret_id()==n)
{
me.show_profile();
flag=1;
}
}
// Explicitly close the file.
my_db.close();
if(my_db.is_open())
{
cout << "\nA file close error occurred.";
}
if(flag==0)
cout<<"\n\tAccount Number does not exist";
_getch();
}
};
int main()
{
my_database db1;
char ch;
do
{
system("cls");
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. NEW profile";
cout<<"\n\n\t02. show profile";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option (1-3) ";
ch=_getche();
system("cls");
switch(ch)
{
case '1': db1.write();
_getch();
break;
case '2': db1.display();
break;
case '3': exit(0);
default : cout<<"\a";
}
}while(ch!='3');
return 1;
}