R
Revman
I'm having problems opening and reading from a file to test a Class.
My diver main.cpp is fairly short but with a longer file open function
// Project #4 -- Main/driver program
#include "daytime.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void openFile (ifstream&);
int main()
{
DayTime Day1, Day2;
ifstream file;
openFile(file);
//DEBUG: test read from the file
int one, two, three;
file >> one >> two >> three;
cout << one << two << three << endl;
//DEBUG END!
Day1 >> file;
Day2 >> file;
// Watch -- not intutitive.
Day1 << Day2;
Day2 << Day1;
return 0;
};
//**************************************************************************
*******
// openFile Function
//**************************************************************************
*******
// Precondition:
// recieves inputstream
// Postcondition:
// Verifies user identified file exists, reprompts if necessary.
// opens the file
void openFile(/* in/out */ ifstream& anyFile)
{
string fileName;
int choice = 0;
// used to test if the file was opened suscessfully
int test;
do
{
test = 0; // assume file will be opened correctly
do
{
cout << "Enter the name of the file to open." << endl;
cin >> fileName;
//Prompt for standard file extension(s), if omitted.
cout << endl << "Choose the type of file to open." << endl;
cout << "1 = TXT" << endl << "2 = DAT" << endl;
cout << "3 = Entered file type with file name" << endl;
cin >> choice;
if (choice < 1 || choice > 3)
{
cout << "Error with file type selection." << endl;
cout << "Restarting open file process" << endl;
}
}
while (choice < 1 && choice > 3);
// add file name extension, if required
switch (choice)
{
case 1 : fileName = fileName + ".txt";
break;
case 2 : fileName = fileName + ".dat";
break;
default: break; // no action necessary already included
};
//try to open the file
anyFile.open(fileName.c_str());
cout << "Attempting to open file" << endl;
if (!anyFile)
{
cout << "Error opening file" << endl;
test = 1;
anyFile.clear();
}
}
while(test == 1);
cout << "File open suscessfully" << endl;
return;
}
My class slecification file has some overloaded operators, required by the
project, is as follows:
//**********************************************************
//*** This is the decelaration file for the daytime class***
//*** Specifications can be found in the daytime.cpp file***
//**********************************************************
#include<fstream>
using namespace std;
class DayTime
{
public:
DayTime(); // default constructor
// used to extract day hour minute to the class private members
void operator>> (/* in */ ifstream& anyFile);
void operator << (DayTime otherDayTime);
// used to output to screen the dayTime
// use to compare see if two insitances of DayTime are equal
bool operator == (DayTime otherDayTime);
// used to determine of one DayTime is less than another DayTime
// Assumes Sunday< Monday< Tuesday< ... < Saturday
bool operator < (DayTime otherDayTime);
private:
enum days {ERROR, Sun, Mon, Tue, Wen, Thu, Fri, Sat} day;
int hour;
int min;
};
The implemation file is as follows:
//**************************************************************************
*******
//*** This is theimplementation file of DayTime class
//**************************************************************************
*******
#include "daytime.h"
#include <iostream>
using namespace std;
// private members of class
// enum days {ERROR, Sun, Mon, Tues, Wen, Thu, Fri, Sat} day;
// int hour;
// int min;
//Class declrations
//**************************************************************************
*******
DayTime:ayTime() // default constructor
// Precondition:
// None
//Postsondition:
// Constructed, day set to ERROR, how and min set to "0"
{
day = ERROR;
hour = 0;
min = 0;
}
//**************************************************************************
*******
void DayTime:perator>> (/* in */ ifstream& anyFile)
// Precondition:
// stream is in day/hour/min order
//0 < Day < 8 , -1 < Hour < 24 , and -1 < Min < 60
//Postcondition:
// Day, hour, and minute are set
{
int inDay;
int inHour;
int inMin;
anyFile >> inDay >> inHour >> inMin;
//debug statement
cout << inDay << inHour << inMin;
switch (inDay) // use inDay to set enum day
{
case 1 : day = Sun;
break;
case 2: day = Mon;
break;
case 3: day = Tue;
break;
case 4: day = Wen;
break;
case 5: day = Thu;
break;
case 6: day = Fri;
break;
case 7: day = Sat;
break;
default: cout << "Error!" << endl;
break;
}
hour = inHour;
min = inMin;
}
//**************************************************************************
*******
void DayTime:perator<<(DayTime otherDayTime)
//Precondition:
// DayTime has been set
//Postcondition:
// private members are displayed to the screen in
// DDD HH:MM 'endl'
//format
{
cout << day << " ";
if (hour < 10)
cout << "0";
cout << hour << ":";
if (min < 10)
cout << "0";
cout << min << endl;
}
//**************************************************************************
*******
bool DayTime:perator==(/* in */ DayTime otherDayTime)
//Precondition:
// Current and otherDayTime are declared
//Postcondition:
// if DayTime == otherDayTime, returns true
// else, returns false
{
return (day == otherDayTime.day &&
hour == otherDayTime.hour &&
min == otherDayTime.min);
}
//**************************************************************************
*******
bool DayTime:perator <(/* in */ DayTime otherDayTime)
//Precondition:
// Current and otherDayTime are declared
//Postcondition:
// if DayTime < otherDayTime, returns true
// else, returns false
{
if (day < otherDayTime.day)
return true;
if (day == otherDayTime.day &&
hour < otherDayTime.hour)
return true;
if (day == otherDayTime.day &&
hour == otherDayTime.hour &&
min <otherDayTime.min)
return true;
return false;
}
What I recieve as output when debugging these files I get the following as
output:
Enter the name of the file to open.
Choose the type of file to open.
1 = TXT
2 = DAT
3 = Entered file type with file name
Attempting to open file
File open suscessfully
15-858993460
-858993460-858993460-858993460Error!
-858993460-858993460-858993460Error!
0 0-858993460:0-858993460
0 0-858993460:0-858993460
Any help is greately appericated.
Revman
My diver main.cpp is fairly short but with a longer file open function
// Project #4 -- Main/driver program
#include "daytime.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void openFile (ifstream&);
int main()
{
DayTime Day1, Day2;
ifstream file;
openFile(file);
//DEBUG: test read from the file
int one, two, three;
file >> one >> two >> three;
cout << one << two << three << endl;
//DEBUG END!
Day1 >> file;
Day2 >> file;
// Watch -- not intutitive.
Day1 << Day2;
Day2 << Day1;
return 0;
};
//**************************************************************************
*******
// openFile Function
//**************************************************************************
*******
// Precondition:
// recieves inputstream
// Postcondition:
// Verifies user identified file exists, reprompts if necessary.
// opens the file
void openFile(/* in/out */ ifstream& anyFile)
{
string fileName;
int choice = 0;
// used to test if the file was opened suscessfully
int test;
do
{
test = 0; // assume file will be opened correctly
do
{
cout << "Enter the name of the file to open." << endl;
cin >> fileName;
//Prompt for standard file extension(s), if omitted.
cout << endl << "Choose the type of file to open." << endl;
cout << "1 = TXT" << endl << "2 = DAT" << endl;
cout << "3 = Entered file type with file name" << endl;
cin >> choice;
if (choice < 1 || choice > 3)
{
cout << "Error with file type selection." << endl;
cout << "Restarting open file process" << endl;
}
}
while (choice < 1 && choice > 3);
// add file name extension, if required
switch (choice)
{
case 1 : fileName = fileName + ".txt";
break;
case 2 : fileName = fileName + ".dat";
break;
default: break; // no action necessary already included
};
//try to open the file
anyFile.open(fileName.c_str());
cout << "Attempting to open file" << endl;
if (!anyFile)
{
cout << "Error opening file" << endl;
test = 1;
anyFile.clear();
}
}
while(test == 1);
cout << "File open suscessfully" << endl;
return;
}
My class slecification file has some overloaded operators, required by the
project, is as follows:
//**********************************************************
//*** This is the decelaration file for the daytime class***
//*** Specifications can be found in the daytime.cpp file***
//**********************************************************
#include<fstream>
using namespace std;
class DayTime
{
public:
DayTime(); // default constructor
// used to extract day hour minute to the class private members
void operator>> (/* in */ ifstream& anyFile);
void operator << (DayTime otherDayTime);
// used to output to screen the dayTime
// use to compare see if two insitances of DayTime are equal
bool operator == (DayTime otherDayTime);
// used to determine of one DayTime is less than another DayTime
// Assumes Sunday< Monday< Tuesday< ... < Saturday
bool operator < (DayTime otherDayTime);
private:
enum days {ERROR, Sun, Mon, Tue, Wen, Thu, Fri, Sat} day;
int hour;
int min;
};
The implemation file is as follows:
//**************************************************************************
*******
//*** This is theimplementation file of DayTime class
//**************************************************************************
*******
#include "daytime.h"
#include <iostream>
using namespace std;
// private members of class
// enum days {ERROR, Sun, Mon, Tues, Wen, Thu, Fri, Sat} day;
// int hour;
// int min;
//Class declrations
//**************************************************************************
*******
DayTime:ayTime() // default constructor
// Precondition:
// None
//Postsondition:
// Constructed, day set to ERROR, how and min set to "0"
{
day = ERROR;
hour = 0;
min = 0;
}
//**************************************************************************
*******
void DayTime:perator>> (/* in */ ifstream& anyFile)
// Precondition:
// stream is in day/hour/min order
//0 < Day < 8 , -1 < Hour < 24 , and -1 < Min < 60
//Postcondition:
// Day, hour, and minute are set
{
int inDay;
int inHour;
int inMin;
anyFile >> inDay >> inHour >> inMin;
//debug statement
cout << inDay << inHour << inMin;
switch (inDay) // use inDay to set enum day
{
case 1 : day = Sun;
break;
case 2: day = Mon;
break;
case 3: day = Tue;
break;
case 4: day = Wen;
break;
case 5: day = Thu;
break;
case 6: day = Fri;
break;
case 7: day = Sat;
break;
default: cout << "Error!" << endl;
break;
}
hour = inHour;
min = inMin;
}
//**************************************************************************
*******
void DayTime:perator<<(DayTime otherDayTime)
//Precondition:
// DayTime has been set
//Postcondition:
// private members are displayed to the screen in
// DDD HH:MM 'endl'
//format
{
cout << day << " ";
if (hour < 10)
cout << "0";
cout << hour << ":";
if (min < 10)
cout << "0";
cout << min << endl;
}
//**************************************************************************
*******
bool DayTime:perator==(/* in */ DayTime otherDayTime)
//Precondition:
// Current and otherDayTime are declared
//Postcondition:
// if DayTime == otherDayTime, returns true
// else, returns false
{
return (day == otherDayTime.day &&
hour == otherDayTime.hour &&
min == otherDayTime.min);
}
//**************************************************************************
*******
bool DayTime:perator <(/* in */ DayTime otherDayTime)
//Precondition:
// Current and otherDayTime are declared
//Postcondition:
// if DayTime < otherDayTime, returns true
// else, returns false
{
if (day < otherDayTime.day)
return true;
if (day == otherDayTime.day &&
hour < otherDayTime.hour)
return true;
if (day == otherDayTime.day &&
hour == otherDayTime.hour &&
min <otherDayTime.min)
return true;
return false;
}
What I recieve as output when debugging these files I get the following as
output:
Enter the name of the file to open.
Choose the type of file to open.
1 = TXT
2 = DAT
3 = Entered file type with file name
Attempting to open file
File open suscessfully
15-858993460
-858993460-858993460-858993460Error!
-858993460-858993460-858993460Error!
0 0-858993460:0-858993460
0 0-858993460:0-858993460
Any help is greately appericated.
Revman