• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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
0
ts420
Asked:
ts420
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now