Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem in writing to a file
    primarykey
    data
    text
    <p>Guys I wrote the following code for implementing a phonebook in c++ What I am doing is first take input from three files containing names, address and phone number(u may not look at the entire code) just look at the bottom</p> <p>Now I give user to add contacts to add during runtime and these values are stored in a class. Now I erase the files containing names, address and numbers and write new data into them using ofstream which will be retrieved when user again runs the "phonebook"</p> <p>however i am not able to see any input into the files after program has run once and I added a few values during the runtime. Can someone help me? Thanks in advance</p> <pre><code> #include&lt;iostream&gt;//Include Header Files #include&lt;cstdlib&gt; #include&lt;fstream&gt; #include&lt;string&gt; 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&gt;&gt;file_input_name){ d[i].name=file_input_name; i++; } initsize=i; f2.open("./numbers"); while(f2&gt;&gt;file_input_number){ d[i1].phonenumber=file_input_number; i1++; } i1=0; f3.open("./address"); while(f3&gt;&gt;file_input_address){ d[i1].address=file_input_address; i1++; } cout&lt;&lt;"\tWelcome to the phone Directory\n";//Welcome Message do{ //do-While Loop Starts cout&lt;&lt;"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&gt;&gt;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&lt;&lt;"\nEnter The Name\n"; cin&gt;&gt;name; if(name==" "){cout&lt;&lt;"Blank Entries are not allowed"; flag_no_blank=true; } }while(flag_no_blank==true); flag_no_blank=false; d[i].name=name; cout&lt;&lt;"\nEnter the Phone Number\n"; cin&gt;&gt;phno; d[i1].phonenumber=phno; cout&lt;&lt;"\nEnter the address\n"; cin&gt;&gt;add; d[i1].address=add; i1++; i++; break;//Exit Case 1 to the main menu } case 2: { cout&lt;&lt;"\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&gt;&gt;name; int k=0,val; cout&lt;&lt;"\n\nSearching.........\n\n"; for(int j=0;j&lt;=i;j++){ if(d[j].name==name){ k++; cout&lt;&lt;k&lt;&lt;".\t"&lt;&lt;d[j].name&lt;&lt;"\t"&lt;&lt;d[j].phonenumber&lt;&lt;"\t"&lt;&lt;d[j].address&lt;&lt;"\n\n"; val=j; } } char ch; cout&lt;&lt;"\nTotal of "&lt;&lt;k&lt;&lt;" Entries were found....Do you wish to edit?\n"; string staticname; staticname=d[val].name; cin&gt;&gt;ch; if(ch=='y'|| ch=='Y'){ cout&lt;&lt;"Which entry do you wish to modify ?(enter the old telephone number)\n"; cin&gt;&gt;phno; for(int j=0;j&lt;=i;j++){ if(d[j].phonenumber==phno &amp;&amp; staticname==d[j].name){ cout&lt;&lt;"Do you wish to change the name?\n"; cin&gt;&gt;ch; if(ch=='y'||ch=='Y'){ cout&lt;&lt;"Enter new name\n"; cin&gt;&gt;name; d[j].name=name; } cout&lt;&lt;"Do you wish to change the number?\n"; cin&gt;&gt;ch; if(ch=='y'||ch=='Y'){ cout&lt;&lt;"Enter the new number\n"; cin&gt;&gt;phno1; d[j].phonenumber=phno1; } cout&lt;&lt;"Do you wish to change the address?\n"; cin&gt;&gt;ch; if(ch=='y'||ch=='Y'){ cout&lt;&lt;"Enter the new address\n"; cin&gt;&gt;add; d[j].address=add; } } } } break; } case 3 : { cout&lt;&lt;"\n\tContents of PhoneBook:\n\n\tNames\tNumbers\tAddresses\n"; for(int t=0;t&lt;=i;t++){ if(d[t].name=="Noname") continue; cout&lt;&lt;".\t"&lt;&lt;d[t].name&lt;&lt;"\t"&lt;&lt;d[t].phonenumber&lt;&lt;"\t"&lt;&lt;d[t].address&lt;&lt;"\n"; } cout&lt;&lt;"\n\n\n\n"; break; } case 4:{ cout&lt;&lt;"Enter a name to search\n"; cin&gt;&gt;name; cout&lt;&lt;"\n\nSearching.........\n\n"; for(int j=0;j&lt;=i;j++){ if(d[j].name==name){ k++; cout&lt;&lt;k&lt;&lt;".\t"&lt;&lt;d[j].name&lt;&lt;"\t"&lt;&lt;d[j].phonenumber&lt;&lt;"\t"&lt;&lt;d[j].address&lt;&lt;"\n\n"; int val=j; } } cout&lt;&lt;"\nA total of "&lt;&lt;k&lt;&lt;" contact names were found having the name"&lt;&lt;name; break; } case 6:{ cout&lt;&lt;"\n\nClosing the phonebook...Visit Again\n"; flag=true; break; } case 5: { cout&lt;&lt;"\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&gt;&gt;name; int k=0,val; cout&lt;&lt;"\n\nSearching.........\n\n"; for(int j=0;j&lt;=i;j++){ if(d[j].name==name){ k++; cout&lt;&lt;k&lt;&lt;".\t"&lt;&lt;d[j].name&lt;&lt;"\t"&lt;&lt;d[j].phonenumber&lt;&lt;"\t"&lt;&lt;d[j].address&lt;&lt;"\n\n"; val=j; } } char ch; cout&lt;&lt;"\nTotal of "&lt;&lt;k&lt;&lt;" Entries were found....Do you wish to delete?\n"; if(k==0) break; string staticname; staticname=d[val].name; cin&gt;&gt;ch; if(ch=='y'|| ch=='Y'){ cout&lt;&lt;"Which entry do you wish to delete ?(enter the old telephone number)\n"; cin&gt;&gt;phno; for(int j=0;j&lt;=i;j++){ if(d[j].phonenumber==phno &amp;&amp; staticname==d[j].name){ val=j; } } for(int j=val;j&lt;=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&lt;&lt;w &amp;&amp; y&lt;=i){ if(w=="Noname") y++; continue; y++; w=d[y].name; } y=0; int v; v=d[0].phonenumber; while(f8&lt;&lt;v &amp;&amp; y&lt;=i){ if(v==0){y++; continue;} y++; v=d[y].phonenumber; } y=0; string u; u=d[0].address; while(f9&lt;&lt;u &amp;&amp; y&lt;=i ){ if(u=="Noaddress"){ continue; y++; } y++; u=d[y].address; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload