Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:12:39+00:00 2026-06-03T09:12:39+00:00

So I’ve been up for a long time now and maybe thats why I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:12:40+00:00Added an answer on June 3, 2026 at 9:12 am

    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)

    void Phone::display_phonebook(ifstream& in_stream)
    {
      while(!in_stream.eof())
        {
          string line;
          getline(in_stream, line);
          cout<<line<<endl;
        }
    }
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.