Needing to write to class and store 5 dates & Store in a Texfile

[code][#include <iostream>
using namespace std;

#define Length 2 // i know about this

class date
{
public:
int dd;
int mm;
int yy;
}; // I think a declaration such as Date [length ] as i want the for loop to iterate and input into the class and the output to output the 5 members of the date class.

int main ()
{
for (int n = 0; n < Length; n++);
{
cin >> date.dd;
cin >> date.mm;
cin >> date.yy;
}

cout << " The Date Stored in the Date Class is ";
cout << " Day : " << date.dd;
cout << " Month : " << date.mm;
cout << " Year : " << date.yy;

system ("pause");
return 0;
}
/code]

Now the basic objectives of this program is to store upto 5 dates inside the date class though i cannot get any input or output as my declartions in the cin and cout statement cannot find the member though i think i am close i am getting frustrated with it.

Then when i have this right i will be inputting from file so i want to make sure that the data is accurate

any help and guidance would be greatly appricated

Thanks
Last edited on
You haven't created an object of the class date.
1
2
3
4
5
6
7
int main()
{
    date dateObject;
    cin >> dateObject.dd;

    return 0;
}


And seeing as you are using a for loop to get input twice for each, you may want to create an array of objects.
date dateObject[ Lenght ];

And use the 'n' from the for loop to itterate through them.
Last edited on
Thanks for that i thought i had done something stupid like not declaring i had though about the array of dates as well though I used similar for a struct though i had not dealt with classes I believe this should solve my issue thanks if I have any issue with this i will post back

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
#include <iostream>
using namespace std;

#define Length 5

class date
{
  public:
    int dd;
    int mm;
    int yy;
};date dateObject[Length];


int main ()
{
    date dateObject;



  for (int n = 0; n <Length; n++)
  {
      cout << " Please Enter in Day";
    cin >>dateObject.dd;
    cout << "Please Enter in Month";
    cin >>dateObject.mm;
    cout << "Please Enter In Year";
    cin >>dateObject.yy;
  }
 for (int n = 0;n<Length;n++)
 {
  cout << " The Date Stored in the Date Class is ";
  cout << " Day : "  <<dateObject.dd;
  cout << " Month : " <<dateObject.mm;
  cout << " Year : " <<dateObject.yy <<endl;
}
  system ("pause");
  return 0;
}


The second program is meant to take in

10 dates seperated by spaces
ie DD MM YYY

each date is on a seperate line
stored in a file called dates.dat

so i am looking at using a file input for 10 spaces stored either in an element called date then outputting the day ,month,year

so i was thinking of something such as

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
#include <iostream>
#include <fstream>
using namespace std;

#define Length 10

class date
{
  public:
    int dd;
    int mm;
    int yy;
    string date;
};date dateObject[Length];


int main ()
{
  date dateObject;
string date;

    
  

  for (int n = 0; n <Length; n++)
  {
    ifstream myfile ("dates.dat");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      myfile >>dateObject.dd;
      myfile >> dateObject.mm;
      myfile>>dateObject.yy;
}
      
    myfile.close();}

}  
 for (int n = 0; n <Length; n++)
 {
  cout << " The Date Stored in the Date Class is ";
cout << "Day :"<< dateObject.dd<<endl;
cout <<"Month :"<<dateObject.mm<<endl;
cout <<" Year :" <<dateObject.yy<<endl;
}
{
  system ("pause");
  return 0;
}}


Though My code only outputs the last line which is also an issue i have found with the top program in this post the issue is reccuring I belive it is storing at equal to the length ie if the length varible is 5 it is taking the 5th value only and returning it 5 times

any help on this would be greatly appricated
Last edited on
Topic archived. No new replies allowed.