c++formatted-text

How to read and process the formatted date like 01/11/1998, Sunday(10000 inputs) C++


I need to read the date with weekday. First of all, i need to read the date and weekday then calculate the total weekday, for example:

total Sunday :1000
total Monday :1000
......

I always get the value is 0. The input file looks like this:

23/10/2005, Sunday
26/07/2016, Tuesday
10/01/1995, Tuesday
14/10/2015, Wednesday
30/09/1982, Thursday
22/09/1993, Wednesday
21/05/1972, Sunday
23/01/2017, Monday
20/05/1974, Monday
27/11/1985, Wednesday
11/07/2005, Monday
06/09/2014, Saturday
16/03/1991, Saturday
09/03/1970, Monday
17/08/2015, Monday
04/05/2010, Tuesday
14/11/2013, Thursday
13/11/2015, Friday
08/10/1995, Sunday
07/09/1986, Sunday
.....

which there is 10000.

string line;

string day[7] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
ifstream infile("input.txt");
ofstream validfile("valid.txt");
ofstream invalidfile("invalid.txt");
int total = 0;
int date[2], month[2], year[4];
int Sunday = 0, Monday = 0, Tuesday = 0, Wednesday = 0, Thursday = 0, Friday = 0, Saturday = 0;
char symbol = '/';
char symbol2 = ',';

while (getline(infile, line)) {
    total = total++;
}
validfile << "Valid file\n" << "The total record :" << total << endl;


    while (!infile.eof()) {

        infile >> day[2] >> symbol >> month[2] >> symbol >> year[4] >> symbol2 >> line;

        if (line.compare(day[0]) == 0) {
            Sunday++;
        }
        else if (line.compare(day[1]) == 0) {
            Monday++;
        }
        else if (line.compare(day[2]) == 0) {
            Tuesday++;
        }
        else if (line.compare(day[3]) == 0) {
            Wednesday++;
        }
        else if (line.compare(day[4]) == 0) {
            Thursday++;
        }
        else if (line.compare(day[5]) == 0) {
            Friday++;
        }
        else if (line.compare(day[6]) == 0) {
            Saturday++;
        }
}

        cout << "Total Sunday :" << Sunday << endl;
        cout << "Total Monday :" << Monday << endl;
        cout << "Total Tuesday :" << Tuesday << endl;
        cout << "Total Wednesday :" << Wednesday << endl;
        cout << "Total Thursday :" << Thursday << endl;
        cout << "Total Friday :" << Friday << endl;
        cout << "Total Saturday :" << Saturday << endl;

Solution

  • After the 1st loop you've reach to end of file.

    After you've reach the end of file you need to go back to the beginning before starting the 2nd loop:

    //this is the 1st loop in your code:
    while (getline(infile, line)) {
        total = total++;
    }
    
    validfile << "Valid file\n" << "The total record :" << total << endl;
    
    //now you need to rewind:
    infile.clear(); //clear EOF state
    infile.seekg(0); //back to beginning
    
    //then continue the 2nd loop
    while (!infile.eof()) {
    

    In addition: you have error in the following line:

    infile >> day[2] >> symbol >> month[2] >> symbol >> year[4] >> symbol2 >> line;
    

    For example: infile >> day[2] will read only one character, not 2. also I guess you write to day instead of to date. a possible solution is to use infile.get(date,2) to read 2 bytes, or to read line and copy substrings.