Output to two out of three files; Help!

So in this assignment we are supposed to read in a bunch of information from "INPUT_DATA.txt" for a publication company. The information is basic stuff like last name, first name, address, city, state, country, expiration date and subscriber number.

The user enters in the current date, and based on the date entered the information is output it to three files: "OUTPUT_FILE.txt" if it is completely valid, "INVALID_FILE.txt" if the expiration date contains a character that isn't a number or space(because I replace all the spaces with 0's), and "EXPIRY_FILE.txt" if the subscription has expired.

The problem I'm having is that it will only output to OUTPUT_FILE and EXPIRY_FILE. Everything put into OUTPUT_FILE is correct, but EXPIRY_FILE somehow gets BOTH the invalid subscriptions and the expired subscriptions. INVALID_FILE is left completely empty!

Can anyone help me out here? This is driving me crazy now. I'm still a little new at this so forgive me if I've made a simple mistake.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
#include <fstream>
using namespace std;

void welcome();
bool validityCheck(string expiration);
bool expiryCheck(string expiration, int today);
int strToInt(string s);

int main()
{
    ifstream input("INPUT_DATA.txt");
    ofstream output("OUTPUT_FILE.txt");
    ofstream invalid("INVALID_FILE.txt", ios::app);
    ofstream expired("EXPIRY_FILE.txt", ios::app);

    string lastName;
    string firstName;
    string address;
    string city;
    string stateprov;
    string country;
    string zip;
    string expiration;
    string subscribeNum;
    bool isItInvalid;
    bool isItExpired;
    int whereTo;
    int currentDate;

    welcome();
    cin >> currentDate;

    while (!input.eof())
    {
        getline (input, lastName);
        getline (input, firstName);
        getline (input, address);
        getline (input, city);
        getline (input, stateprov);
        getline (input, country);
        getline (input, zip);
        getline (input, expiration);
        getline (input, subscribeNum);

        isItInvalid = validityCheck(expiration);
        isItExpired = expiryCheck(expiration, currentDate);

        if(isItInvalid == true && isItExpired == false)
            WhereTo = 1;
        else if(isItInvalid == false && isItExpired == true)
            whereTo = 2;
        else if(isItInvalid == false && isItExpired == false)
            whereTo = 3;

        if(whereTo == 1)
        {
            invalid << lastName << endl;
            invalid << firstName << endl;
            invalid << address << endl;
            invalid << city << endl;
            invalid << stateprov << endl;
            invalid << country << endl;
            invalid << zip << endl;
            invalid << expiration << endl;
            invalid << subscribeNum << endl;
        }

        else if(whereTo == 2)
        {
            expired << lastName << endl;
            expired << firstName << endl;
            expired << address << endl;
            expired << city << endl;
            expired << stateprov << endl;
            expired << country << endl;
            expired << zip << endl;
            expired << expiration << endl;
            expired << subscribeNum << endl;
        }
        else if(whereTo == 3)
        {
            output << lastName << endl;
            output << firstName << endl;
            output << address << endl;
            output << city << endl;
            output << stateprov << endl;
            output << country << endl;
            output << zip << endl;
            output << expiration << endl;
            output << subscribeNum << endl;

            cout << lastName << endl << firstName << endl << address << endl;
            cout << city << endl << stateprov << endl << country << endl;
            cout << zip << endl << expiration << endl << subscribeNum << endl << endl;
        }

    }
    cout << "\n\tAll records have been moved to their designated files!\n";
    cout << "\t\t\tHave a good day!";
}
bool expiryCheck(string expiration, int today)
{
    int expirationNum;
    expirationNum = strToInt(expiration);
    if(expirationNum < today)
        return true;
    else
        return false;
}
int strToInt(string s)
{
    stringstream ss;
    int dateNum;
    ss.str ("");
    ss << s;
    ss >> dateNum;
    return dateNum;
}
bool validityCheck(string expiration)
{
    int i;
        // Replace spaces with zeros
        for(i=0; i < expiration.length(); i++)
        {
            if(expiration[i] == ' ')
                expiration[i] = '0';
        }

        // Check for invalid dates
        for(i=0; i < expiration.length(); i++)
        {
            if (!isdigit(expiration[i]))
                return true;
            else
                return false;
        }

}
void welcome()
{
    cout << "\t********************************************************\n";
    cout << "\t***** Welcome to the Subscription Sorting Program! *****\n";
    cout << "\t********************************************************\n";
    cout << "\tI will sort subscriptions into the three files,\n";
    cout << "\tOUTPUT_FILE.txt, INVALID_FILE.txt, and EXPIRY_FILE.txt\n\n";
    cout << "\tPlease enter the current date in the format YYMMDD: ";
}
Last edited on
I'm pretty sure there's something wrong with how I have it checking to see if the date is invalid or not. Any suggestions?
Topic archived. No new replies allowed.