写入文件时出现问题
|
伙计们,我编写了以下代码,以在C ++中实现电话簿
我正在做的是首先从包含名称,地址和电话号码的三个文件中输入(您可能看不到整个代码),只看底部
现在,我给用户添加要在运行时添加的联系人,并且这些值存储在一个类中。
现在,我删除了包含名称,地址和数字的文件,并使用ofstream将新数据写入其中,当用户再次运行“电话簿”时将检索这些数据。
但是,程序运行一次后,我看不到文件的任何输入,并且在运行时添加了一些值。
有人能帮我吗?
提前致谢
#include<iostream>//Include Header Files
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
class contact{
public:
string name;//ALL CLASS VARIABLES ARE PUBLIC
int phonenumber;
string address;
contact(){//Constructor
name= \"Noname\";
phonenumber= 0;
address= \"Noaddress\";
}
};
int main(){
contact *d;
d= new contact[200];
string name,add;
int choice,modchoice,k=0;//Variable for switch statement
int phno,phno1;
int i=0;
int initsize=0, i1=0;//i is declared as a static int variable
bool flag=false,flag_no_blank=false;
//TAKE DATA FROM FILES.....
//We create 3 files names, phone numbers, Address and then abstract the data from these files first!
fstream f1;
fstream f2;
fstream f3;
string file_input_name;
string file_input_address;
int file_input_number;
f1.open(\"./names\");
while(f1>>file_input_name){
d[i].name=file_input_name;
i++;
}
initsize=i;
f2.open(\"./numbers\");
while(f2>>file_input_number){
d[i1].phonenumber=file_input_number;
i1++;
}
i1=0;
f3.open(\"./address\");
while(f3>>file_input_address){
d[i1].address=file_input_address;
i1++;
}
cout<<\"\\tWelcome to the phone Directory\\n\";//Welcome Message
do{
//do-While Loop Starts
cout<<\"Select :\\n1.Add New Contact\\n2.Update Existing Contact\\n3.Display All Contacts\\n4.Search for a Contact\\n5.Delete a Contact\\n6.Exit PhoneBook\\n\\n\\n\";//Display all options
cin>>choice;//Input Choice from user
switch(choice){//Switch Loop Starts
case 1:{
i++;//increment i so that values are now taken from the program and stored as different variables
i1++;
do{
cout<<\"\\nEnter The Name\\n\";
cin>>name;
if(name==\" \"){cout<<\"Blank Entries are not allowed\";
flag_no_blank=true;
}
}while(flag_no_blank==true);
flag_no_blank=false;
d[i].name=name;
cout<<\"\\nEnter the Phone Number\\n\";
cin>>phno;
d[i1].phonenumber=phno;
cout<<\"\\nEnter the address\\n\";
cin>>add;
d[i1].address=add;
i1++;
i++;
break;//Exit Case 1 to the main menu
}
case 2: {
cout<<\"\\nEnter the name\\n\";//Here it is assumed that no two contacts can have same contact number or address but may have the same name.
cin>>name;
int k=0,val;
cout<<\"\\n\\nSearching.........\\n\\n\";
for(int j=0;j<=i;j++){
if(d[j].name==name){
k++;
cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";
val=j;
}
}
char ch;
cout<<\"\\nTotal of \"<<k<<\" Entries were found....Do you wish to edit?\\n\";
string staticname;
staticname=d[val].name;
cin>>ch;
if(ch==\'y\'|| ch==\'Y\'){
cout<<\"Which entry do you wish to modify ?(enter the old telephone number)\\n\";
cin>>phno;
for(int j=0;j<=i;j++){
if(d[j].phonenumber==phno && staticname==d[j].name){
cout<<\"Do you wish to change the name?\\n\";
cin>>ch;
if(ch==\'y\'||ch==\'Y\'){
cout<<\"Enter new name\\n\";
cin>>name;
d[j].name=name;
}
cout<<\"Do you wish to change the number?\\n\";
cin>>ch;
if(ch==\'y\'||ch==\'Y\'){
cout<<\"Enter the new number\\n\";
cin>>phno1;
d[j].phonenumber=phno1;
}
cout<<\"Do you wish to change the address?\\n\";
cin>>ch;
if(ch==\'y\'||ch==\'Y\'){
cout<<\"Enter the new address\\n\";
cin>>add;
d[j].address=add;
}
}
}
}
break;
}
case 3 : {
cout<<\"\\n\\tContents of PhoneBook:\\n\\n\\tNames\\tNumbers\\tAddresses\\n\";
for(int t=0;t<=i;t++){
if(d[t].name==\"Noname\") continue;
cout<<\".\\t\"<<d[t].name<<\"\\t\"<<d[t].phonenumber<<\"\\t\"<<d[t].address<<\"\\n\";
}
cout<<\"\\n\\n\\n\\n\";
break;
}
case 4:{
cout<<\"Enter a name to search\\n\";
cin>>name;
cout<<\"\\n\\nSearching.........\\n\\n\";
for(int j=0;j<=i;j++){
if(d[j].name==name){
k++;
cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";
int val=j;
}
}
cout<<\"\\nA total of \"<<k<<\" contact names were found having the name\"<<name;
break;
}
case 6:{
cout<<\"\\n\\nClosing the phonebook...Visit Again\\n\";
flag=true;
break;
}
case 5: {
cout<<\"\\nEnter the contact-name\\n\";//Here it is assumed that no two contacts can have same contact number or address but may have the same name.
cin>>name;
int k=0,val;
cout<<\"\\n\\nSearching.........\\n\\n\";
for(int j=0;j<=i;j++){
if(d[j].name==name){
k++;
cout<<k<<\".\\t\"<<d[j].name<<\"\\t\"<<d[j].phonenumber<<\"\\t\"<<d[j].address<<\"\\n\\n\";
val=j;
}
}
char ch;
cout<<\"\\nTotal of \"<<k<<\" Entries were found....Do you wish to delete?\\n\";
if(k==0) break;
string staticname;
staticname=d[val].name;
cin>>ch;
if(ch==\'y\'|| ch==\'Y\'){
cout<<\"Which entry do you wish to delete ?(enter the old telephone number)\\n\";
cin>>phno;
for(int j=0;j<=i;j++){
if(d[j].phonenumber==phno && staticname==d[j].name){
val=j;
}
}
for(int j=val;j<=i-1;j++){
d[j].name=d[j+1].name;
d[j].phonenumber=d[j+1].phonenumber;
d[j].address=d[j+1].address;
}
d[i].name=\"Noname\";
d[i].phonenumber=0;
d[i].address=\"Noaddress\";
}
break;
}
}
}
while(flag==false);
std::ofstream f4(\"./names\");
f4.close();
std::ofstream f5(\"./numbers\");
f5.close();
std::ofstream f6(\"./address\");
f6.close();
f1.close();
f2.close();
f3.close();
ofstream f7,f8,f9;
f7.open(\"names\");
f8.open(\"numbers\");
f9.open(\"address\");
int y=0;
string w;
w=d[0].name;
while(f7<<w && y<=i){
if(w==\"Noname\") y++; continue;
y++;
w=d[y].name;
}
y=0;
int v;
v=d[0].phonenumber;
while(f8<<v && y<=i){
if(v==0){y++; continue;}
y++;
v=d[y].phonenumber;
}
y=0;
string u;
u=d[0].address;
while(f9<<u && y<=i ){
if(u==\"Noaddress\"){
continue;
y++;
}
y++;
u=d[y].address;
}
return 0;
}
没有找到相关结果
已邀请:
4 个回复
祁澈盘哼颗
草票
绊伦欧
)正好是200条记录。 坦白说,此代码应移至TheDailyWTF。 Pronto Pronto! 此代码无法修复。期
浩挎