Hi, I'm a newbie to this site, but I will soon be an active member. Currently I'm writing a program where one of my methods has to read data from a text file and set it to an array.
I then need to display that data in the console, but I only want to display what there is to display. Right now, it lists 45 items (the maximum amount it should display), but it will list blank lines after it has finished listing whatever data there was, and I need it to stop once there is no more data.

One person told me to make it loop until the new variable = the old variable,
but I really don't understand how to do this.

Here's what I got:

void ViewData()
{
    int x=0;
    int y=0;
    int sum = 0;
    int n = 0;
    double TotalOwed=0.00;
     
    char cMemberNumber [1000];
    string MemberNumber [1000];
    char cMemberName [1000];
    string MemberName [1000];
    char cSchool [1000];
    string School [1000];
    char cYearJoined [1000];
    string YearJoined [1000];
    char cAccountBalance [1000]; //char
    string AccountBalance [1000]; //double?? string
    
    
    
    //int TotalAmount [1000];

       
       
          
    ifstream OpenFile("data.txt", ios::in);
	
		// Reads the data into the program
		while (!OpenFile.eof())
		{
              
                          x++;
                          OpenFile.getline(cMemberNumber,2000);
                          MemberNumber [x] = cMemberNumber;
                          OpenFile.getline(cMemberName,2000);
                          MemberName [x] = cMemberName;
                          OpenFile.getline(cSchool,2000);
                          School [x] = cSchool;
                          OpenFile.getline(cYearJoined,2000);
                          YearJoined [x] = cYearJoined; 
                          OpenFile.getline(cAccountBalance,2000);
                          AccountBalance [x] = cAccountBalance;
                          
                          //TotalAmount [1000] = atof(AccountBalance [1000]);
        }
        OpenFile.close();
     
     
     cout << "Current Account Balances: " << endl;
     cout << endl;
     cout << setw(18) << "MemberNumber" << setw(15) << "MemberName" << setw(13) <<  "School" << setw(18) << "Year Joined" <<
          setw(15) << "Amount Owed" << endl; 
     
     

      do
      {
     x++;
     y++;
     cout << endl;
     cout << "Line " << x << ":" << setw(5) << MemberNumber [y] << setw(18) << MemberName [y] << setw(18) << School [y] << setw(12) << YearJoined [y] << 
             setw(15) << AccountBalance [y] << endl;
     
     
     }while(x<=44);
     //cout << endl;
     //cout << "Total Amount Owed: " << TotalOwed << endl;
     system("pause");   

        


}

I don't see that the code you posted would display anything. In the reading loop, variable x counts how many records were read. Actually, it will have a count 1 too high based on the way you've set your loop control with the eof( ) method.

Then, in the display loop, you keep incrementing x, and testing it against some arbitrary number 44. If there were 44 or 45 records, this loop would, at best, show one then quit.

Using the count value stored in x, your display loop should start its count with y=0 and use the test while (y < x) Don't change the variable that's holding your count of records.

In your use of the getline( ) function, you pass a parameter of 2000 (size of destination array) while your actual arrays are only 1000 in size. Be consistent and correct. Further, why have several uniquely named input variables, that immediately get copied to the actual storage strings? You could use (and reuse) just one input array. Better, why not just read directly into the destination strings? getline( OpenFile, MemberNumber [x] );

Please read this about using .eof() the way you are (it's the same as using feof() .

And read this about formatting your code so it can be understood.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.