C++ text file into array + help
Oct 22, 2012 at 8:19pm UTC
I'm doing a program where i need to put a text file that has a list of dates that look like this.
Text file:
10 20 2011
12 5 2009
7 16 2008
etc..
and i need to put that into a array.
I can put the information in the array but it shows up like
10
20
2011 etc.
is there a way i can put the whole month/day/year on one line and count for 1 as a whole in an array?
thanks in advance
Oct 22, 2012 at 8:28pm UTC
Yes. create a struct that holds three integers and create array of the struct then.
Store each number from the date in the respective member of the struct.
Oct 22, 2012 at 9:29pm UTC
I have this and not really good when it comes to structures since im new to it.
it compiles and the output is at the bottom. Help would be deeply appreciated.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct calender {int month;
int day;
int year;
};
int main()
{
calender array[20] , *ptr;
ifstream dates("dates.txt" );
while (!dates.eof())
{
for (ptr = &array[0]; ptr < &array[20]; ptr++)
{
dates >> (*ptr).month;
dates >> (*ptr).day;
dates >> (*ptr).year;
}
for (int i=0;i<=20;i++)
{
cout << &array[4] << endl;
}
}
system ("pause" );
return 0;
}
output:
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
0x28fe70
Topic archived. No new replies allowed.