Hey all, having a bit of a problem with a recent project. The goal here is to be able to input several lines of text containing a date in mm/dd/yyyy format followed by a whitespace and then a description of the event. I've accomplished actually allowing for multiple lines of text input (I think), but now I'm not exactly sure how to stop it. Is there a way to simply assume after a set amount of time, perhaps, that the user is done inputting? This problem is stressing me a lot as they never really addressed how to end user input, only how to accomplish multiple lines. We have to use the standard user input, not from text files which would be so much easier. Code below, please feel free to also tell me if I've made an unnoticed mistake elsewhere. I just need to be able to let the user finish the input so the program can re-order the events[] and output them.
calendar.cpp
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
#include "event.h"
int main(){
// Maximum number of input lines
const int MAX = 100;
// Array of pointers to events
Event *events[MAX];
// Number of currently used pointers in events
int size = 0;
int i = 0;
// Vector containing each input line as a string
vector<string> vec (100);
char temps[100];
while(i < MAX){
cin.getline(temps, 100);
vec[i] = temps;
i++;
}
for(int i = 0; i < vec.size(); i ++){
int found = vec[i].find("/");
int tmo = atoi(vec[i].substr(0, found).c_str());
int sfound = vec[i].find("/", found + 1, 6);
int tda = atoi(vec[i].substr(found + 1, sfound - (found + 1)).c_str());
int tye = atoi(vec[i].substr(sfound + 1, 4).c_str());
string tdes = vec[i].substr(sfound + 6, vec[i].length() - (sfound + 6));
events[size] = new Event(tmo, tda, tye, tdes);
size++;
}
return 0;
}
event.cpp
#include <iostream>
#include <string>
using namespace std;
#include "event.h"
Event::Event(int mo, int da, int ye, string des){
month = mo;
day = da;
year = ye;
desc = des;
}
Event::~Event(){
}
void Event::print(){
cout << month << "/" << day << "/" << year << " " << desc << endl;
}
event.h
#ifndef EVENT_H
#define EVENT_H
#include <iostream>
#include <string>
using namespace std;
class Event{
public:
Event(int mo, int da, int ye, string des);
~Event();
void print();
private:
int month;
int day;
int year;
string desc;
};
#endif
Thank you in advance for your help, gentlemen.
EDIT:
Replacing my while() loop with:
while ((i < MAX) && (!cin.eof()) {
cin.getline(temps, 100);
vec[i] = temps;
i++;
}
Seemed promising but even after injecting an EOF marker with Ctrl + D (^D) as suggested, user input can still continue. Any reason why this might be happening?