Taking input from a file and storing it in a vector

Hi all! I'm working on a project for my C++ class that is essentially a calendar. One of the features of this calendar is to read user "appointment" files from a text file in the form of mm/dd/yyyy "Descriptive string" or mm/dd "Holiday" and attach them to the calender (Each appointment is on a new line). I tried approaching this task simply using ist >> struct.int >> struct.int etc etc for (int,int,int,string), but I ran into some major issues -
1. Even if I try to overload the operator, it doesn't recognize \ as a delimiter and essentially crashes after the first int.
2. This method and struct do not account for the form mm/dd "Holiday" whatsoever
3. White spaces in event names like "St Patrick's Day" get saved in the vector as simply "St"
Is this approach essentially flawed for this type of problem?
Here's 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
struct Apt_Date{
	int day, year;
	int m;
	string title;
};
istream &operator >> (istream &ist, Apt_Date &apts)
{
	ist.ignore(1,'/');
}
	

int main (){
	string temp_file_name, line, response, apt;
	cout << "\nFile Name: ";
	cin >> temp_file_name; 
	ifstream ist;
	Apt_Date apts;
	vector<Apt_Date> appointments;
	ist.open(temp_file_name.c_str(), ios::app);
	if (ist.is_open()) {
		while(!ist.eof()){
			ist >> apts.day >> apts.m >> apts.year >> apts.title;
			cout << apts.day << "/" << apts.m << "/" << apts.year << " " << apts.title << "\n";
			appointments.push_back(apts);
			getline(ist,line);
		}
	}
else cout << "Unable to open file"; 
return 0;	
}


Any help would be greatly appreciated! I assure you all I am not simply trying to "get answers for my homework" but rather be pointed in the right direction of a fix to my current approach or a different approach entirely. Thanks!
a) Your overloaded operator is wrong
b) Your overloaded operator isn't even called
1) You can go different ways:
Use getline(ist, <temp_string>, '/'); and then std::stoi() to convert string to int.
Ignore '/' manually after each input operation.
Read whole line, replace '/' with whitespaces, create stringstream out of it and read from it.
2) You should check if there '/' ater you read month.
3) USe getline() to read whole line with whitespaces
Last edited on
Topic archived. No new replies allowed.