Reading and comparing data from file.

I have an input file with lines in different formats.
**************************************************************
Sample of Input File:

Matthew Alan Aberegg 1978 Account$: 452,627
Adams, William Logan 1957 50,767
Siyabi, Mohamed born: 1970 790,183
Sulaiman, Yasser 1951 696,311
Barnett, Alan Christopher 1976 Account$: 83,736
Catherine P Baugher 1979 552,067
**************************************************************
I need to look at the date and if they are sixty or older print out "ready" at the end of the line.
I've been working on this for hours and I can't figure it out. It will produce a blank output file, all the numbers together or print out only the first line. How can I get it to read the numbers correctly? (i.e. as a four digit number) so I can compare it to 1956 which is the most recent birth date that would have "ready" printed at the end of its line.
************************************************************************
Sample Program:

ifstream input;
ofstream output;
output.open("newdata.txt);
input.open("data.txt");
char c[10000];
string line;
int i=0;

while(input)
{
for(i=0;i<10000;i++)
{
input.get(c[i]);

if(isdigit(c[i]); //Around here is where I'm stuck.
{
output<<c[i];
}
}
}
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

const int    DATENOTFOUND = -1;
const string NAMENOTFOUND = "****";


int extractDate( string line )       // Gets date as a 4-digit integer
{
   string delimiter = "0123456789";
   int start = line.find_first_of( delimiter);   if ( start == string::npos ) return DATENOTFOUND;
   string dateString = line.substr( start, 4 );
   int dateInt = atoi( dateString.c_str() );
   return dateInt;
}


string extractName( string line )    // Gets surname, but you probably need more
{
   string delimiter = ", ";
   int end = line.find_first_of( delimiter );   if ( end == string::npos ) return NAMENOTFOUND;
   return line.substr( 0, end );
}


int main()
{
   int dateInt;
   string name;
   string line;
   string status;

   ifstream in( "data.txt" );
   ofstream out( "newdata.txt" );

   while( getline( in, line ) )
   {
      name = extractName( line );
      dateInt = extractDate( line );
      status = dateInt <= 1956 ? "READY" : "";

      cout << name << "  " << dateInt << endl;
      out << line << "  "  << status << endl;
   }
   in.close();
   out.close();
}
First suggestion. Since the data in the file is arranged in separate lines, then read it line by line.
1
2
3
4
5
6
7
    ifstream input("data.txt");
    string line;

    while (getline(input, line))
    {
        cout << line << '\n';
    }


Second suggestion. Use a stringstream to parse the data within the line.
1
2
3
    std::istringstream ss(line);
    int year = 0;
    ss >> year;

This fragment shows how to read an integer from the line. However - that is only a start. In practice there are several names or words which precede the year within the line.

There are lots of different ways to do this, You could for example read character-by-character until a digit is found, then use the putback() function to replace that digit into the stringstream where you got it from, and then read the year using ss >> year.
1
2
3
    char ch = ' ';
    while (ss.get(ch) && !isdigit(ch))
        ; // empty loop 

http://www.cplusplus.com/reference/istream/istream/putback/

Another approach would be use a loop. Keep trying until either the year has been read, or the stream is empty. If the read of the integer failed, try to read a string instead. Hint, you will need to clear() the error flags after a failed read of the integer.

Those are just ideas of course. You may have others.
Last edited on
read every char until you find a 1 or 2, if you find a 2 go to next line
if you find 1, check next number to see if it's > 5, if yes check next to see if it's > 6.

if you want a 4 digit year read them all and put together with + symbol.

Topic archived. No new replies allowed.