Im doing a program assignment where i need to:
Create a minimal DateType class which provides at least these operations:
1) SetDate to assign a value to an existing DateType object (parameters of month, day,
and year).
2) An input function that would read dates directly in the format used by the input file.
3) Output to display a date in the format mm/dd/yyyy.
4) Compare two dates for == and > (separate functions).
I have started with the program and for right now, my question is how do i get the information that i have on one function and send it to another function. In this case im trying to get the information of readDate and put it into printDate. Any help would be appriciated.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
class DateType
{
public:
struct calender
{
int month;
int day;
int year;
};
calender array[20];
void setDate(int month, int day, int year);
int readDate()
{
ifstream dates("dates.txt");
while (!dates.eof())
{
for( int i=0; i < 20;i++)
{
dates >> array[i].month;
dates >> array[i].day;
dates >> array[i].year;
}
}
}
void printDate( int readDate() ) // this is where i need help.
{
for( int i=0; i < 20;i++) // i was experimenting with it.
{
cout << array[i].month << "/" << array[i].day << "/" << array[i].year << endl;
}
}
};
int main()
{
DateType myDate;
myDate.printDate();
system("pause");
return 0;
}
// void DateType::readDate()
also im not the best c++ person so don't make fun of me =D
#include <iostream>
#include <fstream>
usingnamespace std;
class DateType
{
struct calender // Doesn't need to be public, no one outside of the class is going to use it
{
int month;
int day;
int year;
};
calender array[20]; // Warning, array is a reserved keyword and represents something new in C++11
void readDate() // made this a void function, could also be private
{
ifstream dates("dates.txt");
for( int i=0; !dates.eof() && i < 20;i++) // Combining while and for loops for better logic
{
dates >> array[i].month;
dates >> array[i].day;
dates >> array[i].year;
}
}
public:
void printDate( ) // don't call functions in the argument list
{
readDate(); // function calls go down here
for( int i=0; i < 20;i++) // i was experimenting with it.
{
cout << array[i].month << "/" << array[i].day << "/" << array[i].year << endl;
}
}
};
int main()
{
DateType myDate;
myDate.printDate();
system("pause");
return 0;
}