0

I'm compiling my code using g++ on a mac using terminal. The majority of my program runs without errors, but there are two specific spots where I get an error.

  1. The first spot where I get an error is when the user enters a number to delete a completed task. The weird thing is sometimes that portion of my program works, but other times I get an error.

  2. The second spot where I get an error is when the user adds tasks to their to-do list for a second time.

enter image description here

This is the first spot in my program where I get an error ^

enter image description here

This is the second spot in my program where I get an error ^

Lastly, here's my code:

#include <iostream>
#include <vector>
using namespace std;

void addToDoItems(vector<string>(toDoList));

void deleteToDoItems(vector<string>(toDoList));

int main(){
   vector<string> toDoList; //vector that stores user's tasks needed completion
   
   addToDoItems(toDoList); //calls function so user can add tasks to to-do list

   return 0;
} 

void addToDoItems(vector<string>(toDoList)) {
   int count = 1;
   char capitalizeFirstLetter;
   string userInput;

   cout << "\nPlease enter a task that you would like added to your to-do-list: ";
   getline(cin, userInput);
   cout << endl;

   while (userInput != "q") {
      capitalizeFirstLetter = toupper(userInput.at(0)); //capitalizes first letter of first word of a task
      userInput.erase(0,1);
      userInput = to_string(count) + ". " + capitalizeFirstLetter + userInput; //adds modified task with correct capitalization to vector
      toDoList.push_back(userInput); //adds user task to list of things to do
      cout << "Add another task or press q if you are done: ";
      getline(cin, userInput);
      cout << endl;
      ++count;
   }

   cout << "Here's what you need to accomplish today:\n" << endl;

   for (int i = 0; i < toDoList.size(); ++i) { //outputs the user's tasks needed to be done
      cout << toDoList.at(i) << endl;
   }

   cout << "\nPlease press d if you would like to check off your items on your to-do list or press any other key to be done: ";
   cin >> userInput;
   cout << endl;

   if (userInput == "d") {
      deleteToDoItems(toDoList); //calls functions so user can delete tasks completed
   }

   else {
      cout << "Have a good day!\n" << endl; //ends program
   }
}

void deleteToDoItems(vector<string>(toDoList)) {
   int taskNumToDelete;
   int count = 1;
   string tasksLeft;
   string userInput;

   cout << "Please enter the number of the task that you have completed (refer to list above): ";
   cin >> taskNumToDelete;
   cout << endl;

   while (taskNumToDelete != -1) {

      toDoList.erase(toDoList.begin()+(taskNumToDelete)-1); //deletes on of the tasks in the vector based on the number the user entered

      cout << "Please enter the number of the next task that you have accomplished (enter -1 to see remaining tasks if applicable): ";
      cin >> taskNumToDelete;
      cout << endl;
   }

   if (toDoList.size() > 0) {

      if (toDoList.size() == 1) {
         cout << "You are so close to being done! You have " << toDoList.size() << " more task to do!\n" << endl;   
      }

      else {
         cout << "You are so close to being done! You have " << toDoList.size() << " more tasks to do!\n" << endl;
      }

      for (int i = 0; i < toDoList.size(); ++i) { //outputs remaining tasks
         tasksLeft = toDoList.at(i);
         tasksLeft.erase(0,1); //erases orignal number of task

         cout << count << tasksLeft << endl; //outputs the remaining tasks with new numbers
         ++count;
      }

      cout << endl;
      cout << "Please press d to check more items off, press a to add more items to your list, or press any other key to be done: ";
      cin >> userInput;
      cout << endl;

      if (userInput == "d") {
         deleteToDoItems(toDoList); //calls function if user wants to delete more completed tasks
      }

      else if(userInput == "a") {
         addToDoItems(toDoList); //calls function if user wants to add more tasks
      }

      else {
         cout << "Have a good day!\n" << endl; //ends program
      }

   }

   else {
      cout << "Congratulations! You finished all your tasks! Now you can relax :)\n" << endl;
   }

}
  • 1
    Those look like errors you get when running the program, not when compiling it. To your error: After you've deleted two tasks from your vector, and you tell it that you've finished task 3, it tries to call `toDoList.erase(toDoList.begin() + 2)` when `toDoList` only contains 1 element. Does that make sense? – Nathan Pierson Dec 05 '22 at 02:15
  • 1
    Regarding your second issue, take a look at [why std::getline "skips" input after a formatted extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). You're mixing formatted extractions like `cin >> taskNumToDelete;` with line-based extractions like `getline(cin, userInput);` – Nathan Pierson Dec 05 '22 at 02:20

0 Answers0