'Reading lines from data file not returning the correct number of lines?

I made a conceptual program to add to my larger program, but I'm having an issue with a counter.

I have found plenty of excellent recourses on how to count lines in a data file (using various methods) I've tried all of them.

What is happening is, that I set up 3 arrays to hold various data. Wrote that data to a file.. and now. Before I read the data back I want to be able to know the size ( or how many lines are in the file).

I can print out all of the data from the file but the counter keeps only being set as 1.

In my larger program the number or lines will not be known to begin with.



#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;

class ReadWrite
{
  public:
    ofstream OutputFile,temp;
    ifstream InputFile;
    string taskID[3]={"1","2","3"};
    string task[3]={"blah1","blah2","blah3"};
    string date[3]={"11/11/1111", "12/12/1212","13/13/1313"};
    string line[3];
    string lines;
    int count=0;
    
    void write2file();
    void readfile();
    void remove();
};
  void ReadWrite::readfile()
  {
    InputFile.open("TODOs.txt");
    if (!InputFile.is_open())
    {
      cout<<"No saved TODOs.\n";
    }
    else
    {
      cout<<"TODOs uploading.\n";
      while(InputFile>>lines);
      {
        cout<<lines<<endl;
        count++;
      }
      InputFile.close();
      InputFile.open("TODOs.txt");
      cout<<"lines: "<<count<<endl;
      for (int i=0;i<3;i++)
      {
        while(getline(InputFile,line[i]))
        {
          cout<<line[i]<<endl;
        }
      }
      InputFile.close();
    }
  }
  void ReadWrite::write2file()
  {
    OutputFile.open("TODOs.txt");
    for(int i=0; i<3;i++)
    {
    OutputFile<<taskID[i]<<"\t"<<date[i]<<"\t"<<task[i]<<'\n';
    }
    OutputFile.close();
  }
 

int main()
{
  ReadWrite rw;


  bool progLoop = true;
  do
  {
    // SimpAlphaMenu sam(choices, len);
    // int choice = sam.showMenu();
    char choice; 
    cin>>choice;
    if (choice=='+')
    {
      rw.write2file();
      progLoop=true;
    }
      
    else if (choice=='-')
    {cout<<"You are in -\n";}
    else if (choice=='?')
    {       
      rw.readfile();
      progLoop=true;
    }
    else
      progLoop = false;
  
  }while(progLoop);
}





After editing in suggestions from @Retired Ninja the code seems to be working fine.

void ReadWrite::readfile()   {
    InputFile.open("TODOs.txt");
    if (!InputFile.is_open())
    {
      cout<<"No saved TODOs.\n";
    }
    else
    {
      cout<<"TODOs uploading.\n";

        while(getline(InputFile,lines))  //Edited this line here
        {
          // cout<<lines<<endl;
          count++;
        }
      InputFile.close();
      InputFile.open("TODOs.txt");
      cout<<"lines: "<<count<<endl;
      for (int i=0;i<3;i++)
      {
        while(getline(InputFile,line[i]))
        {
          cout<<line[i]<<endl;
        }
      }
      InputFile.close();
    }   }
c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source