Y2k program

Having some issues getting this to compile. This is an assignment that requires the use of class.
For this method, you will read in 10 dates from the file entitled dates.dat and display them to the GUI.  The dates are stored in the format mmddyy (e.g., May 1, 1992 would be converted to 050192).  You will then perform date expansion and write the expanded dates to the GUI and to the file expand.dat. Basically from 2 digit years to 4 digit years.

/* Expands Y2K dates to a disk file */

#include <iostream.h>  
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>

class Y2K {
int main(void)
{
public:
  char ch;

  ifstream inFile;
  inFile.open("dates.dat", ios::in);

  if(inFile.fail())
  {
    cout << "Error opening input file dates.dat" << endl;
    exit(1);
  }

  ofstream outFile;
  outFile.open("expand.dat", ios::out);

  if(outFile.fail())
  {
    cout << "Error opening output file expand.dat" << endl;
    exit(1);
  }

  inFile >> ch;         // Read 1st char
  while(!inFile.eof()) // While not at the end of the file
  {
       inFile >> ch;      // Read 1st char
       outFile << ch;  // Write 1st char to expand.dat
       cout << ch;      // Write 1st char to screen

       inFile >> ch;      // Read 2nd char
       outFile << ch;  // Write 2nd char to expand.dat
       cout << ch;      // Write 2nd char to screen

       inFile >> ch;      // Read 3rd char
       outFile << ch;  // Write 3rd char to expand.dat
       cout << ch;      // Write 3rd char to screen
      
       inFile >> ch;      // Read 4th char
       outFile << ch;  // Write 4tht char to expand.dat
       cout << ch;      // Write 4th char to screen
        
        outFile << "19";      //Write 5th and 6th char to expand.dat
      cout << "19";      //Write 5th and 6th char to screen
      
       inFile >> ch;      // Read 5th char
       outFile << ch;  // Write 7th char to expand.dat
       cout << ch;      // Write 7th char to screen

       inFile >> ch;      // Read 6th char
       outFile << ch;  // Write 8th char to expand.dat
       cout << ch;      // Write 8th char to screen
 
       outFile << endl;// Write newline char to expand.dat
       cout << endl;   // Write newline char to screen

       inFile >> ch;   // Read next 1st char
  }

  inFile.close();
  outFile.close();

  return 1;
} // end -main()
} // end -Y2K Class
ts420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IndrawatiCommented:
class Y2K {
int main(void)
{
public:
  char ch;

  ifstream inFile;
  inFile.open("dates.dat", ios::in);

How can a class contain main()? If you change the above code to:

int main(void)
{
  char ch;

  ifstream inFile;
  inFile.open("dates.dat", ios::in);

it will be compilable.
0
AxterCommented:
Your class also needs to have a semicolon at the end.
}; // end -Y2K Class
0
AxterCommented:
FYI:
Both <iostream.h> and <fstream.h> are not part of the C++ standard.

I recommend you use the extensionless version which is part of the C++ standard, and is portable.

#include <iostream>  
#include <fstream>

using namespace std;

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

itsmeandnobodyelseCommented:
Look at that:

// y2k.h

#ifndef Y2K_H
#define Y2K_H

#include <string>

using namespace std;

class Y2k
{
      string strInFile;
      string strOutFile;

public:

      Y2k(const string& inpfile, const string& outfile)
          :  strInFile(inpfile), strOutFile(outfile) {}

      bool run();
               
};

#endif

//-------------------------------------------------------
// y2k.cpp

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

using namespace std;

#include "y2k.h"

bool Y2k::run()
{
  ifstream inFile(strInFile.c_str());
  if(inFile.fail())
  {
      cout << "Error opening input file dates.dat" << endl;
      return false;
  }

  ofstream outFile(strOutFile.c_str());
  if(outFile.fail())
  {
      inFile.close();
      cout << "Error opening output file expand.dat" << endl;
      return false;
  }

  bool ret = true;
  string line;
  while (getline(inFile, line))
  {
      string date;
      if (line.length() == 6)
      {
          date = line.substr(0, 4);
          int yy = (line[4]-'0')*10 + (line[5]-'0');
          if (yy > 20 && yy < 100)
              date += "19";
          else if (yy >= 0 && yy < 20)
              date += "20";
          else
          {
              cout << "Error: invalid input date " << line << endl;
              ret = false;
              break;
          }
          date += line.substr(4, 2);
          outFile << date << endl;
          cout << date << endl;
      }
      else
      {
          cout << "Error: invalid input date " << line << endl;
          ret = false;
          break;
      }
  }
  inFile.close();
  outFile.close();

  return ret;
}

int main()
{
    Y2k y2k("dates.dat", "expand.dat");
    if (y2k.run())
        cout << "Successfully converted dates.dat to expand.dat." << endl;
    else
        cout << "There are errors while converting dates.dat to expand.dat." << endl;
    return 0;
}

Regards, Alex





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rstaveleyCommented:
Funny what an anti-climax the Y2K bug was. Seems like ancient history now.
0
AxterCommented:
>>Funny what an anti-climax the Y2K bug was. Seems like ancient history now.

It kept some of us programmers employed... :-)
0
ts420Author Commented:
Thank you, I was way off.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.