请参阅下面的更新!
我的代码(现在我包含更多):
while(getline(checkTasks, workingString)){
countTheDays = 0;
// Captures a clean copy of the line in tasks.dat in it's entirety, stores in workingString.
char nlcheck;
checkTasks.get(nlcheck);
if(nlcheck == '\n'){
}
else{
checkTasks.unget();
//getline(checkTasks, workingString);
// Breaks that line up into more usable pieces of information.
getline(check2,tName, '\t');
getline(check2,tDate, '\t');
getline(check2,trDate, '\t');
getline(check2,tComplete, '\n');
// Converts the string form of these pieces into usable integers.
stringstream(tDate.substr(0,tDate.find_first_of('/'))) >> year;
stringstream(tDate.substr(tDate.find_first_of('/')+1,tDate.find_last_of('/'))) >> month;
stringstream(tDate.substr(tDate.find_last_of('/')+1,tDate.length()-1)) >> day;
stringstream(tComplete) >> checkComplete;
stringstream(trDate) >> checkReminder;
// Adds the number of days until your task is due!
if(year != date[0]){
for(int i = date[1]; i <= 12; i++){
countTheDays += System::DateTime::DaysInMonth(date[0], i);
}
countTheDays-= date[2];
for (int i = 1; i<= month; i++){
countTheDays +=System::DateTime::DaysInMonth(year, i);
}
countTheDays -= (System::DateTime::DaysInMonth(year, month) - day);
}
else if(month != date[1]){
for(int i = date[1]; i <= month; i++){
countTheDays += System::DateTime::DaysInMonth(date[0], i);
}
countTheDays -= (date[2]);
countTheDays -= (System::DateTime::DaysInMonth(year, month) - day);
}
else{
countTheDays+= System::DateTime::DaysInMonth(date[0], month);
countTheDays -= (System::DateTime::DaysInMonth(year, month) - day);
countTheDays -= date[2];
}
// If the task is nearing it's due date (the time frame specified to be notified) it'll notify user.
// Only coded to work if the task is due in the current or following months.
if(countTheDays <= checkReminder){
if( countTheDays < 0){
cout << endl << endl << tName << " is past due!" << endl;
cout << "Should I keep reminding you about this task? Enter Y or N. ";
cin >> continueToRemind;
}
else{
cout << endl << endl << tName << " is due in " <<countTheDays << " days! Don't forget!" << endl;
cout << "Should I keep reminding you about this task? Enter Y or N. ";
cin >> continueToRemind;
}
// If user doesn't want to be reminded, begins process of converting that line in the file to usable info
// and 'overwriting' the old file by creating a new one, deleting the old one, and renaming the new one.
if(continueToRemind == "n" || continueToRemind == "N"){
fileModified = true;
string line;
/* vector<string> lines;
while(getline(tasksClone, line)){
lines.push_back(line);
}
lines.erase(remove(lines.begin(), lines.end(), workingString), lines.end());
if (!lines.empty()) {
auto i=lines.begin();
auto e=lines.end()-1;
for (; i!=e; ++i) {
saveTasks << *i << '\n';
}
saveTasks << *i;
}*/
// This writes a copy of the tasks.dat file, minus the task that the user elected not to be notified of.'
while(getline(tasksClone, testBuffer)){
if(testBuffer == workingString){
// This condition does nothing. Essentially erasing the 'completed' task from the list.
}
else if(testBuffer != workingString && tasksClone.eof()){
// This writes everything except the specified task to taskbuffer.dat
saveTasks << testBuffer;
}
else {
saveTasks << testBuffer << '\n';
}
}
}
}
}
}
}
else{
cout << "The tasks file is empty, you must not have any tasks!" << endl;
}
我希望这个问题是有道理的!
谢谢
马库斯
更新:再次修改代码。在告诉它删除 1 个任务后,它永远不会运行此循环。当前代码:
while(getline(tasksClone, testBuffer)){
if(testBuffer == workingString){
// This condition does nothing. Essentially erasing the 'completed' task from the list.
}
else if(testBuffer != workingString && tasksClone.eof()){
// This writes everything except the specified task to taskbuffer.dat
saveTasks << testBuffer;
}
else {
saveTasks << testBuffer << '\n';
}
}
输入文件:
test1 2012/12/13 10 0;
test2 2012/12/23 20 0;
test3 2012/12/31 28 0;
\n
输出文件(告诉它删除test1和2之后):
test2 2012/12/23 20 0;
test3 2012/12/31 28 0;
\n