Al è qui
25-01-2002, 18:31
Ho provato a compilare questo listato, ma oltre a fare una fatica bestiale per compilarlo correttamente, una volta che lo uso ha comportamente assolutamente inspiegabili, come un loop sulla richiesta se continuare o meno con il progz.
Richiede:
continuare? con le opzioni Y e N
se metto s
si impalla e spara righe sul terminale in modo infinito.
Vi metto il listato:
--
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include<alloc.h>
#include<iostream.h>
class person
{
public:
char name[20];
int age;
person()
{
age=0;
strcpy(name,"");
};
void getdata()
{
cout<<"Enter your name::";
cin>>name;
cout<<"Enter your age::";
cin>>age;
}
void showdata()
{
cout<<"NAME:::"<<name<<endl;
cout<<"AGE:::"<<age;
cout<<endl<<endl;
}
};
void main()
{
int choice,flag=0,age1;
char name1[20],ch='y',name2[20];
person pers;
fstream file,temp;
do
{
file.open("file.txt",ios::app|ios::out|ios::in);
temp.open("temp.txt",ios::app|ios::out|ios::in);
cout<<"**********MAIN MENU**********"<<endl<<endl;
cout<<" Press 1 to add a record"<<endl<<endl;
cout<<" Press 2 to read a record"<<endl<<endl;
cout<<" Press 3 to edit a record"<<endl<<endl;
cout<<" Press 4 to delete a record"<<endl<<endl;
cout<<" Press 5 to quit"<<endl<<endl;
cout<<"Enter::";cin>>choice;
switch(choice)
{
case 1:
do
{
pers.getdata();
file.write((char *)&pers,sizeof(pers));
cout<<"Do you want to continue::"<<endl;
cin>>ch;
}while(ch=='y' || ch=='Y');
continue;
case 2:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 3:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name you want to edit::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
cout<<"Enter new name::";
cin>>name2;
cout<<"Enter new age::";
cin>>age1;
strcpy(pers.name,name2);
pers.age=age1;
temp.write((char *)&pers,sizeof(pers));
file.seekg(0);
cout<<"heloooooooo";
while(!file.eof())
{
file.read((char *)&pers,sizeof(pers));
if(strcmp(pers.name,name1)!=0)
{
temp.write((char *)&pers,sizeof(pers));
file.read((char *)&pers,sizeof(pers));
}
}
file.close();
temp.close();
system("del file.txt");
system("ren temp.txt file.txt");
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 4:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name you want to delete::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
file.seekg(0);
while(!file.eof())
{
file.read((char *)&pers,sizeof(pers));
if(strcmp(pers.name,name1)!=0)
{
temp.write((char *)&pers,sizeof(pers));
file.read((char *)&pers,sizeof(pers));
}
}
file.close();
temp.close();
system("del file.txt");
system("ren temp.txt file.txt");
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 5:
break;
default:
break;
}
}while(choice!=5);
}
--
Avete idee?
Compilato con Borland e Dev-C++
Al
Richiede:
continuare? con le opzioni Y e N
se metto s
si impalla e spara righe sul terminale in modo infinito.
Vi metto il listato:
--
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include<alloc.h>
#include<iostream.h>
class person
{
public:
char name[20];
int age;
person()
{
age=0;
strcpy(name,"");
};
void getdata()
{
cout<<"Enter your name::";
cin>>name;
cout<<"Enter your age::";
cin>>age;
}
void showdata()
{
cout<<"NAME:::"<<name<<endl;
cout<<"AGE:::"<<age;
cout<<endl<<endl;
}
};
void main()
{
int choice,flag=0,age1;
char name1[20],ch='y',name2[20];
person pers;
fstream file,temp;
do
{
file.open("file.txt",ios::app|ios::out|ios::in);
temp.open("temp.txt",ios::app|ios::out|ios::in);
cout<<"**********MAIN MENU**********"<<endl<<endl;
cout<<" Press 1 to add a record"<<endl<<endl;
cout<<" Press 2 to read a record"<<endl<<endl;
cout<<" Press 3 to edit a record"<<endl<<endl;
cout<<" Press 4 to delete a record"<<endl<<endl;
cout<<" Press 5 to quit"<<endl<<endl;
cout<<"Enter::";cin>>choice;
switch(choice)
{
case 1:
do
{
pers.getdata();
file.write((char *)&pers,sizeof(pers));
cout<<"Do you want to continue::"<<endl;
cin>>ch;
}while(ch=='y' || ch=='Y');
continue;
case 2:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 3:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name you want to edit::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
cout<<"Enter new name::";
cin>>name2;
cout<<"Enter new age::";
cin>>age1;
strcpy(pers.name,name2);
pers.age=age1;
temp.write((char *)&pers,sizeof(pers));
file.seekg(0);
cout<<"heloooooooo";
while(!file.eof())
{
file.read((char *)&pers,sizeof(pers));
if(strcmp(pers.name,name1)!=0)
{
temp.write((char *)&pers,sizeof(pers));
file.read((char *)&pers,sizeof(pers));
}
}
file.close();
temp.close();
system("del file.txt");
system("ren temp.txt file.txt");
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 4:
file.seekg(0);
do
{
file.seekg(0);
cout<<"Enter the name you want to delete::";
cin>>name1;
file.read((char *)&pers,sizeof(pers));
while(!file.eof())
{
if(strcmp(pers.name,name1)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
file.read((char *)&pers,sizeof(pers));
}
if (flag==1)
{
pers.showdata();
file.seekg(0);
while(!file.eof())
{
file.read((char *)&pers,sizeof(pers));
if(strcmp(pers.name,name1)!=0)
{
temp.write((char *)&pers,sizeof(pers));
file.read((char *)&pers,sizeof(pers));
}
}
file.close();
temp.close();
system("del file.txt");
system("ren temp.txt file.txt");
cout<<"Do you want to continue::";
cin>>ch;
if (ch=='y' || ch=='Y')
continue;
else
break;
}
else
{
cout<<"Record does not exist"<<endl;
cout<<"Do you want to continue::"<<endl;
cin>>ch;
if (ch=='y' || ch=='Y')
{
file.close();
file.open("file.txt",ios::app|ios::out|ios::in);
continue;
}
else
break;
}
}while(ch=='y' || ch=='Y');
continue;
case 5:
break;
default:
break;
}
}while(choice!=5);
}
--
Avete idee?
Compilato con Borland e Dev-C++
Al