c++ struct/array and sorting from text file (dates)

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.

heres my program so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace 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.

again, any help would be appreciated.

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:

1
2
while( dates >> array[x].month >> array[x].day >> array[x].year)
   x++;



Last edited on
thanks for the fast response but I'm still in the same spot. I don't know how to sort an array/struct. Thats why I'm here. Again thanks in advance
Then why didn't you answer the question I asked in my last post?


Do you know how to sort an array of int?
Topic archived. No new replies allowed.