0

伙计们,我编写了以下代码用于在 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

4 回答 4

2

C++ 会自动处理 I/O 错误吗?精神上的?如果不是,那么您的错误返回处理程序在哪里?只是问问。

编辑以回应 OP 的评论:是的,我明白,但通常如果数据没有写入文件,文件系统会返回一个错误代码,试图告诉你为什么它没有被写入。但是您决定忽略文件系统所说的内容。我希望能给你一个提示的问题应该是:

“如果你不检查文件写入调用返回码,你将有一个很好的、长途的尝试来调试你的程序。请检查那些错误返回码并告诉我们它们是什么。这是标准和必需的编程实践,毕竟,如果你不遵循标准,必修课,那么你唯一的希望就是咨询街角的吉普赛算命先生。

“所以:每个文件 I/O 调用返回的代码是什么?”

试试看,告诉我们更多。感谢您的反对:我需要那个。

于 2011-04-16T14:06:41.573 回答
2

顺便提一句。

while(f7<<w && y<=i){
    if(w=="Noname") 
        y++;          // <- proper indention is king
    continue;
    y++;              // <- never reached
    w=d[y].name;      // <- never reached
}

while(f8<<v && y<=i){
    if(v==0) {
        y++; 
        continue;
    }
    y++;
    v=d[y].phonenumber;
}

while(f9<<u && y<=i ){
    if(u=="Noaddress") {
        continue;
        y++;         // <- never reached
    }
    y++;
    u=d[y].address;
}
于 2011-04-16T15:30:20.520 回答
2

我明白你为什么感到沮丧,程序员。这是令人沮丧的代码。

while(flag==false)(根据乔纳森的评论,这并不像看起来那么糟糕。仍然很难从其他混乱中判断真正的垃圾...... :)

怎么回事?只是不要按“6”退出,否则你会免费炸你的 CPU

无论如何,它仍在消耗 100% 的 CPU 并以危险的速度填充 /tmp。我想,它不被称为无限循环(因为它会在文件系统满时立即退出)。感谢上帝,/tmp 在 tmpfs(大小 4g)上,我有 8g 内存可用 :)

将姓名、号码和地址存储在单独的文件中……嗯可能是个好主意。

一次测试运行,我让它写了 3.7G 到地址 :) ("NoaddressNoaddressNoaddressNoaddressNo....") 让它崩溃,因为在加载时,接收地址的缓冲区(显然称为d)正好是 200 条记录。

坦率地说,这段代码应该转移到 TheDailyWTF。嘭嘭嘭!

此代码无法修复。时期

于 2011-04-16T15:07:54.527 回答
0

这个程序有很多问题。让我们尝试修复一些开始,然后在此基础上进行构建。

首先,名字是什么样的?它是否有空格,例如“Fred Jones”,或者可能有逗号,例如“Jones,Fred”。也许像“Chan Kong Sang”这样的很多部分。

如果你允许空格,那么你不能用“<<”读入它。也许你可以每行放一个名字,然后用 getline() 读入。

尝试编写一个只读取名称并存储它们的小程序。您可以使用编辑器创建文件。一旦成功,我们就可以在此基础上进行构建。

于 2011-04-16T16:22:23.633 回答