Hello, I am fairly inexperienced with coding and have been having trouble with a program I am trying to write. The problem is that when reading from a text file the program will only read some of the file not all of it. Any help would be greatly appreciated.
Here is the code:

   int i = 0, cycle = 0, end = 0, year = 0, month = 0, count = 0;
   CALL_DATA listd[100];
   ifstream file ("list.txt");

   if (file.is_open())
      {
         while (file.good())
            {
               file >> listd[i].list;
               file >> listd[i].number;
               file >> listd[i].lcmonth;
               file >> listd[i].lcyear;
               i++;
            }
         file.close();
      }
   for (year = 0;year < 200; year++)
      {
         for (month = 0; month < 13; month++)
            {
               for (i = 0; i < 100; i++)
                  {
                     if (listd[i].list[i] == '\0')
                        break;
                     if (listd[i].lcyear == year && listd[i].lcmonth == month)
                        {
                           if (count == 3)
                              {
                                 cout << endl;
                                 count = 0;
                              }
                           cout << listd[i].list;
                           count++;
                           if (listd[i].number != 0)
                              cout << " " << listd[i].number;
                        }
                  }
            }
      }
   cout << endl;
   return;

And here is the .txt file:

Alabama
0
0
0
Alaska
0
0
0
Arizona
0
0
0
Arkansas
0
0
0
California
0
0
0
Colorado
0
0
0
Connecticut
0
0
0
Delaware
0
0
0
Florida
0
0
0
Georgia
0
0
0
Hawaii
0
0
0
Idaho
0
0
0
Illinois
0
0
0
Indiana
0
0
0
Iowa
0
0
0
Kansas
0
0
0
Kentucky
0
0
0
Louisiana
0
0
0
Maine
0
0
0
Maryland
0
0
0
Massachusetts
0
0
0
Michigan
0
0
0
Minnesota
0
0
0
Mississippi
0
0
0
Missouri
0
0
0
Montana
0
0
0
Nebraska
0
0
0
Nevada
0
0
0
New Hampshire
0
0
0
New Jersey
0
0
0
New Mexico
0
0
0
New York
0
0
0
North Carolina
0
0
0
North Dakota
0
0
0
Ohio
0
0
0
Oklahoma
0
0
0
Oregon
0
0
0
Pennsylvania
0
0
0
Rhode Island
0
0
0
South Carolina
0
0
0
South Dakota
0
0
0
Tennessee
0
0
0
Texas
0
0
0
Utah
0
0
0
Vermont
0
0
0
Virginia
0
0
0
Washington
0
0
0
West Virginia
0
0
0
Wisconsin
0
0
0
Wyoming
0
0
0

Sorry for the length. What am I doing wrong?

Until we see what the CALL_DATA struct or class looks like, we can't really know for sure.

Sorry, I forgot I defined that somewhere else, here it is:

struct CALL_DATA
{
    string list;
    int number;
    int lcmonth;
    int lcyear;
};

Edited 4 Years Ago by SuburbanSamurai

The problem is that when reading from a text file the program will only read some of the file not all of it

Your while( file.goof()) loop is probably choking on the the New Hampshire record. Notice the white space delimiter between New and Hampshire. Thus, you're inserting New in the list field and Hampshire in the number field which is an integer field. So, you're successfully reading all the records prior to the New Hampshire record.

New Hampshire
0
0
0

To read a whole line (including whitespace) into a string, use std::getline()
http://www.cplusplus.com/reference/string/getline/

To read the contents of a file, where each data set consists of the name of a state followed by three numbers, the code would be something like:

std::ifstream file ("list.txt") ;
std::string name_of_state ;
int number ;
int month ;
int year ;

// check success or failure *after* (and not before) the attempted input.
// this is idiomatic - while we have read some data successfully ...  
while( std::getline( file, name_of_state ) && file >> month >> year )
{
    // discard everything up to and including the next newline
    file.ignore( 1024, '\n' ) ; // *** see note below

    // validate month, year etc.

    // use the data that has been read.
}

Note: to understand why std::istream::ignore() is needed, see:
http://www.cplusplus.com/forum/general/69685/#msg372532

I just tried that and now for some reason it is only reading the file up to Delaware. I understand that the space between new hampshire would have been a problem for the original code and if that was the hang up point I would have tried getline to solve the problem but it doesn't get that far. I have tried remaking the txt file thinking maybe that was corrupted somehow but that didn't fix it either. As far as I can tell the code is sound it just doesn't want to do what it's told for some reason.

OMG, nevermind, I am a complete moron. The problem is resolved. In the code I was checking for exit with:

if (listd[i].list[i] == '\0')
   break;

Which will of course end after a while when i reaches a high enough number to have a blank space. I replaced it with:

if (listd[i].list[0] == '\0')
   break;

and it works like a charm. Thanks so much for your help everyone.

This should work:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct CALL_DATA
{
    string list;
    int number;
    int lcmonth;
    int lcyear;
};

int main()
{
    int i = 0;
    CALL_DATA listd[100];

    ifstream file ("list.txt");

    if (file.is_open())
    {
        while (file.good())
        {
            char temp = 0;

            getline(file, listd[i].list);

            // The file could have ended with a newline symbol meaning the last line was empty.
            // I didn't knew how you created your file so I added this check. You can remove it if needed.
            if (listd[i].list.length() != 0)
            {
                file >> listd[i].number;
                file >> listd[i].lcmonth;
                file >> listd[i].lcyear;

                while (file.get() != '\n' && file);

                cout << "List: " << listd[i].list
                     << "\n\tnumber:  "  << listd[i].number
                     << "\n\tlcmonth: " << listd[i].lcmonth
                     << "\n\tlcyear:  "  << listd[i].lcyear
                     << endl << endl;

                i++;
            }
        }
        file.close();
    }

    cout << "Amount of entries read: " << i << endl;

    return 0;
}
This question has already been answered. Start a new discussion instead.