Reading from a txt file, saving it into a parallel array and predicting the future date

Hey, so as part of an assignment, I have to write a program that "predicts the date, given a day" so for example, if I want to know what date it will be 5 days from now. As part of the assignment, we were given a txt file that the program must read from, and we were also given a mandatory function to use.

The text file looks like this...
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
31 28 31 30 31 30 31 31 30 31 30 31

this is the mandatory function:

unsigned int getDayOfYear()
{
time_t t = time(0); // get time now
struct tm* timeinfo = localtime(&t); // populate time structure with current time
return timeinfo->tm_yday; // get the current day of the year
}

And this is what I have 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
  #include <ctime>
#include <iostream>
#include <fstream>//the header file that helps you to actually use the ifstream

using namespace std;
	
//Function Prototypes...................................
unsigned int getDayOfYear();


//Main function...........................................
int main()
{
	//string month[2][12] = {{months}{days}}
	//cout<< month[][];
	
	cout<<"I am the Incredible Date Predictor! \n";
	cout<<"************************************* \n";
	
	
	ifstream inFile;//ifstream is allowing us to read from a file, and inFile is basically just giving it a name
	inFile.open("calender.txt");//directing the class to the exact file to be opened
		
		//this is a precaution to display an error massege when the file cannot be accessed. it prevents the program from crashing
		if(inFile.fail())
		{
			cerr<<"Error Opening File"<<endl;
			exit(1);
		}
		
		
		string month;
		//int days;
		
		while(inFile >> month) // >> days)
		{
			cout << month<< endl;//<< " " << days <<endl;
		}
		
		
		inFile.close();
	
	cout<<"Please enter the number of days in the future you want to predict the day for: "<<endl;
	
	getDayOfYear();

	
	cout<<"Behold! The day you have asked for is: "<<endl;
	return 0;
}

//Function definitions.........................................
// returns the  current day of the year
unsigned int getDayOfYear()
{
	time_t t = time(0);   // get time now
	struct tm* timeinfo = localtime(&t); // populate time structure with current time
	return timeinfo->tm_yday; // get the current day of the year
}
Hello Ndutta,

You have to read a file, but you are not doing it well. I would consider using "std::getline()" to read each line and use a string stream to break up the months and days/month in a vector or array for later use. What you have now will read and print everything until you reach "EOF". It does not make much sense to do that.

The function will return the day of the year from January 1st. Easy enough to add to, but you will need to create a full date from this single number. This is where the file you read will come int use.

For an added feature you could check for leap year and February to 29. Which is a good idea for future programs.

Line 43 asks the user for an entry, but you never get anything from the user.

Line 45 is half right because you never store the return value into anything.

Between lines 45 and 48 you will need to do some more processing to show the new date not just a number.

Hope that helps,

Andy
Thank you Handy Andy. I'm still trying to figure it out, but I hope I'll be done by the end of the day
Topic archived. No new replies allowed.