c++ using a functions info for another function

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.

heres my code

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
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace 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
Last edited on
It's hard to read with 55% empty lines and crazy indenting, but what you would normally do is store the permanent information as members of the class:
1
2
3
4
5
6
7
class MyClass
{
  int a; // member
public:
  void inputA() { cin  >> a; }
  void printA() { cout << a; }
};


Ergo: you're on the right track
Last edited on
I think this is what you were trying to do:
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
#include <iostream>
#include <fstream>

using namespace 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;
}
Last edited on
You are a life saver !! thanks. Sorry about all the empty space, It's easier for me to find out whats happening. Again thanks alot!!
Any time!
nice q and a, helped me too XD
Topic archived. No new replies allowed.