Note that there are some explanatory texts on larger screens.

plurals
  1. POFile I/O Code in C++
    primarykey
    data
    text
    <p>So I've been up for a long time now and maybe thats why I can't figure this out. But my code here will work only on the first execution, i.e., since there is a menu with about 4 options, it will work only for the one that is selected first. When the do while loop kicks in and the menu is displayed again, no matter what you select, it keeps re displaying the menu. I have analyzed the do while loop but am pretty sure there aren't any problems with that. I've only recently started learning about File I/O so maybe there's something I missed. Any help would be really appreciated. Thanks.</p> <p>Here's the code:</p> <p><strong>Phonebook.h</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; using namespace std; class Phone { public: void display_phonebook(ifstream&amp; in_stream);// phonebook is the text file void display_backup(string a[], int size);// backup copy is a string array void datacopy(ifstream&amp; in_stream, string a[]);// to copy the phonebook to the array int numberOfLines(ifstream&amp; in_stream);// to check number of lines in the text file }; </code></pre> <p><strong>Phonebook.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; #include "Phonebook.h" using namespace std; void Phone::datacopy(ifstream&amp; in_stream, string a[]) { int i=0; while(in_stream.good()) { string line; getline(in_stream, line); a[i]=line; i++; } int s=i; for(int x=0;x&lt;s;x++) { cout&lt;&lt;a[x]&lt;&lt;endl; } } int Phone::numberOfLines(ifstream&amp; in_stream) { int count=0; while(!in_stream.eof()) { string line; getline(in_stream, line); count++; } return count; } void Phone::display_phonebook(ifstream&amp; in_stream) { while(!in_stream.eof()) { string line; getline(in_stream, line); cout&lt;&lt;line&lt;&lt;endl; } } void Phone::display_backup(string a[], int size) { for(int i=0;i&lt;size;i++) { cout&lt;&lt;a[i]&lt;&lt;endl; } cout&lt;&lt;endl; } </code></pre> <p><strong>main.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; #include "Phonebook.h" using namespace std; int main() { Phone p; int size=0; ifstream fin; ofstream fout; char file[50], ch; string backup[50]; int flag=0; do { cout&lt;&lt;"Enter the name of the file: "&lt;&lt;endl; cin&gt;&gt;file; fin.open(file); cout&lt;&lt;endl; if(fin.fail()) { cout&lt;&lt;"File not found!"&lt;&lt;endl&lt;&lt;endl; cout&lt;&lt;"Try Again? (Y/N)"&lt;&lt;endl; cin&gt;&gt;ch; if(ch=='N' || ch=='n') { cout&lt;&lt;"Terminating..."&lt;&lt;endl; system("PAUSE"); exit(1); } } else { flag=1; } } while((ch=='Y' || ch=='y') &amp;&amp; flag==0); cout&lt;&lt;"Success! File Opened"&lt;&lt;endl&lt;&lt;endl; int choice; do { cout&lt;&lt;"1 - Display phonebook"&lt;&lt;endl; cout&lt;&lt;"2 - Display backup copy"&lt;&lt;endl; cout&lt;&lt;"3 - Update backup copy"&lt;&lt;endl; cout&lt;&lt;"4 - Exit"&lt;&lt;endl; cout&lt;&lt;"Enter your choice: "; cin&gt;&gt;choice; if(choice==1) { p.display_phonebook(fin); } else if(choice==2) { size=p.numberOfLines(fin); p.display_backup(backup, size); } else if(choice==3) { p.datacopy(fin, backup); } } while(choice!=4); fin.close(); fout.close(); system("PAUSE"); return 0; } </code></pre>
    singulars
    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