0

I am making a contact management system on console. I am at beginner level. I have stored data in file using file handling[class : newContact, function : void newcIn (in my code)]. When i display all data stored in my file, it shows an extra entity. I have used while loop with condition !read1.eof().`

    //create new contact
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
 
ofstream write1, write2;
ifstream read1, read2;
 
class newContact {
      protected:
        string name,num;

      public:
        newContact() {
            name="No name";
            num="1234567891011";    
        }

        void newcIn() {
            write1.open("contact.txt",ios::out|ios::app);
            
            cout << "Enter name : ";
            cin.ignore();
            getline(cin,name);
            cout << "Enter number : ";
            getline(cin,num);
            
            write1 << name << endl;
            write1 << num << endl;
            
            write1.close();
            system("CLS");
        }

        void dispAll() {
            read1.open("contact.txt",ios::in);
            
            while (!read1.eof()) {
                   getline(read1,name);
                   getline(read1,num);
            
                   cout << "Name : " << name << endl;
                   cout << "Number : " << num << endl;
            }
            
            cout << "\n\n--------\n" << endl;
            read1.close();
        }
};

class whatTodo : public newContact {
      public:
     
        void mainMenuWorking() {    
             int mainMenuChoice=0;

             do {
                cout << "Enter your choice : " << endl;         
                cout << "1.New contact\n2.Display All contacts\n3.Exit"<<endl;
                cin >> mainMenuChoice;

                switch (mainMenuChoice) {
                case 1:
                        system("CLS");
                        newcIn();
                        break;
                case 2:
                        system("CLS");
                        dispAll();
                        break;  
                default:
                        cout << "Invalid input" << endl;        
            }
        } while (mainMenuChoice!=0);
    }
};

int main() {
    newContact obj;
    whatTodo obj2;
    
    obj2.mainMenuWorking();
    return 0;
}
TheArchitect
  • 1,160
  • 4
  • 15
  • 26
  • `while(!read1.eof())` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Sep 18 '20 at 16:53
  • 1
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Sep 18 '20 at 16:53

1 Answers1

0

This is happening due to the extra line empty line is inserted by newcIn(). You can remove endl after the contact number writing. It should work for you.

    while (!read1.eof())
    {
        string name1, num1;
        getline(read1, name1);
        getline(read1, num1);

        if (!name1.empty() && !num1.empty())
        {
            cout << "Name : " << name1 << endl;
            cout << "Number : " << num1 << endl;
        }
    }

Due to extra empty line file return empty data but class variable return whatever they have. So, better to add local variable to read data from file.

Suggestion: You can rewrite the newCIn() which can insert the data pair correctly without adding extra linebreak.

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24