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

ところで。

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

C++はI/Oエラーを自動的に処理しますか?精神的に?そうでない場合、エラーリターンハンドラーはどこにありますか?ちょうど同じように。

OPのコメントに応じて編集:はい、わかりましたが、通常、データがファイルに書き込まれていない場合、ファイルシステムは、書き込まれなかった理由を通知しようとするエラーコードを返します。しかし、あなたはファイルシステムが言わなければならなかったことを無視することに決めました。私の質問は、あなたにヒントを与えることを望んでいましたが、次のようにすべきでした。

「file-write-callリターンコードをチェックしないと、プログラムをデバッグするのに長い時間がかかります。これらのエラーリターンコードをチェックして、それらが何であるかを教えてください。これは標準で必要なプログラミング手法です。 、結局のところ、そしてあなたが標準的な、必要な練習に従わないならば、あなたの唯一の望みは角にあるジプシー占い師に相談することです。

「SO:すべてのファイルから返されるコードは何ですか-I / O呼び出し?」

それを試して、詳細を教えてください。そして反対票に感謝します:私はそれが必要でした。

于 2011-04-16T14:06:41.573 に答える
2

コーダーの皆さんがイライラしている理由がわかります。これはイライラするコードです。

while(flag==false) (ジョナサンのコメントによると、これは見た目ほど露骨に悪いわけではありません。残りの混乱から本当のがらくたを判断するのが難しいのはまだ悪いことです... :)

なんてこと?'6' を押して終了しないでください。そうしないと、CPU をタダで揚げることになります。

とにかく、CPU の 100% を消費し、危険なペースで /tmp を埋めていました。おそらく、これは無限ループとは呼ばれていません (ファイルシステムがいっぱいになるとすぐに終了するため)。/tmp は tmpfs (サイズ 4g) にあり、8g の RAM が利用可能です :)

名前、番号、住所を別々のファイルに保存する... うーん、おそらく良い考えです。

1 回のテスト実行で、アドレスに 3.7G を書き込んでみました :) ("NoaddressNoaddressNoaddressNoaddressNo....") ロード時にアドレスを受信するバッファ (明らかに と呼ばれるd) は正確に 200 レコードであるため、クラッシュさせるのは簡単なことではありません。

率直に言って、このコードは TheDailyWTF に移動する必要があります。プロントプロント!

このコードは修正できません。限目

于 2011-04-16T15:07:54.527 に答える
0

このプログラムには多くの問題があります。最初にいくつかを修正してから、それに基づいて構築してみましょう。

まず、名前はどのように見えますか?「Fred Jones」のようにスペースを入れたり、「Jones,Fred」のようにコンマを入れたりしますか? 「Chan Kong Sang」のような部分が多いかもしれません。

スペースを許可すると、「<<」で読み込めなくなります。たぶん、1 行に 1 つの名前を入れて、getline() で読み込むことができます。

名前を読み込んで保存するだけの小さなプログラムを書いてみてください。エディタを使用してファイルを作成できます。それが機能したら、その上に構築できます。

于 2011-04-16T16:22:23.633 に答える