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.
Here’s the code:
Phonebook.h
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Phone
{
public:
void display_phonebook(ifstream& in_stream);// phonebook is the text file
void display_backup(string a[], int size);// backup copy is a string array
void datacopy(ifstream& in_stream, string a[]);// to copy the phonebook to the array
int numberOfLines(ifstream& in_stream);// to check number of lines in the text file
};
Phonebook.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include "Phonebook.h"
using namespace std;
void Phone::datacopy(ifstream& 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<s;x++)
{
cout<<a[x]<<endl;
}
}
int Phone::numberOfLines(ifstream& in_stream)
{
int count=0;
while(!in_stream.eof())
{
string line;
getline(in_stream, line);
count++;
}
return count;
}
void Phone::display_phonebook(ifstream& in_stream)
{
while(!in_stream.eof())
{
string line;
getline(in_stream, line);
cout<<line<<endl;
}
}
void Phone::display_backup(string a[], int size)
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
cout<<endl;
}
main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#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<<"Enter the name of the file: "<<endl;
cin>>file;
fin.open(file);
cout<<endl;
if(fin.fail())
{
cout<<"File not found!"<<endl<<endl;
cout<<"Try Again? (Y/N)"<<endl;
cin>>ch;
if(ch=='N' || ch=='n')
{
cout<<"Terminating..."<<endl;
system("PAUSE");
exit(1);
}
}
else
{
flag=1;
}
}
while((ch=='Y' || ch=='y') && flag==0);
cout<<"Success! File Opened"<<endl<<endl;
int choice;
do
{
cout<<"1 - Display phonebook"<<endl;
cout<<"2 - Display backup copy"<<endl;
cout<<"3 - Update backup copy"<<endl;
cout<<"4 - Exit"<<endl;
cout<<"Enter your choice: ";
cin>>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;
}
1) You shouldn’t post this much code. You should post a minimal complete example; that is, whittle down the code as much as you can while still generating the bad behavior. Eventually either the bug will become obvious or you’ll arrive at something much smaller and simpler for us to comb through.
2) You’re too exhausted to remove all the irrelevant code? No problem, just don’t write it in the first place. Start small, build up, test at every stage, and never add to code that doesn’t work. You should never have let the code get this big without discovering the problem.
3)
This will display the contents of the file once. Then the file stream is at the end of the file, like staring at the back cover of a book. When you call the function again, it gives you nothing more. You must either store the contents of the file in a variable, or close and reopen the stream (or rewind, but that’s a more advanced technique, not recommended).