I'm having some trouble with this program I have to. I have to read in a text file containing dates and input them in a struct/array and sort the dates either greatest to least or least to greatest. I've put the month/date/year in a struct but i dont know how i would tell the computer to sort the dates. Any help would be greatly appreciated. I don't expect people to do the program for me. I'm just looking for a little push.
Also the program has to see if there is 2 or more of the same date and delete the extra.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
struct calender // making the struct for the dates
{ //day , month , and year
int month;
int day;
int year;
};
int main()
{
int max = 19;
calender array[max];
ifstream dates("dates.txt");
while (!dates.eof())
// while ( dates.good()) // loop to read each day month and year 20 times to print out each 20 dates
{
for ( int x =0 ; x < 19; x++)
{
dates >> array[x].month;
dates >> array[x].day;
dates >> array[x].year;
cout << array[x].month << "/" << array[x].day << "/" << array[x].year << endl; // print out dates
}
}
system("pause");
return 0;
}
[output]
10/13/2011
7/4/2011
2/14/2010
5/31/2009
12/25/2010
2/2/2011
10/31/2010
12/25/2011
1/1/2008
7/4/2011
7/4/2009
1/1/2008
12/25/2010
12/25/2011
1/1/2010
5/31/2009
12/24/2011
2/2/2009
2686360/4247478/4247248
[/output]
another problem is it outputs that huge number at the end and I don't know why but that isnt a big problem for me right now.
I've put the month/date/year in a struct but i dont know how i would tell the computer to sort the dates.
Do you know how to sort an array of int?
another problem is it outputs that huge number at the end and I don't know why but that isnt a big problem for me right now.
There are a couple of problems with your code that could be causing this problem. First unless you are using compiler specific extensions the use of Variable Length Arrays are not allowed in a C++ program, so you shouldn't be doing the following:
1 2
int max = 19;
calender array[max];
In C++ when declaring arrays you must use a compile time const for the size. So you should either make max a const or better yet use a vector instead of the array.
The next possible problem is with your data entry loop. What happens if there are less than 19 dates in your file. Also you really don't need both the while loop and the for statement. But I really recommend using the while statement, but don't use eof() to control your loop, use the read operation. Something like: