0

I am trying to obtain the date from CLI. However, I am first getting it as a string because the program misbehaves when I input in some invalid value e.g., a string while using std::cin >> directly on an int. Here is my code so far and it is not behaving as expected. After entering in the date in mm/dd/yyyy format, it still shows "We could not interpret your input". Here is my code below, any help would be greatly appreciated:

  while (true)
{
      bool success = true;
      std::cout << "Enter the date added to inventory in the format mm/dd/yyyy: ";
      std::string mm, dd, yyyy;
      std::string temp;
      std::getline(std::cin,temp);
      for (int i=0; i<temp.size(); i++)
        {
          if ((temp[i]=='/') || (temp[i]='\\') || (temp[i] == '-'))
              temp[i] = ' ';
        }
      std::stringstream datestream(temp);
      datestream >> mm >> dd >> yyyy;
      try {
      date->month = static_cast<char>(std::stoi(mm.c_str()));
      date->day = static_cast<char>(std::stoi(dd.c_str()));
      date->year = static_cast<int>(std::stoi(yyyy.c_str()));
      } catch (std::invalid_argument e)
        {
          std::cout << "We could not interpret your input. \n";
          success = false;
        } catch (std::out_of_range e)
        {
          std::cout << "Your input was too large. Please try a smaller number a smaller number for dd, mm, and yyyy. \n";
          success = false;
        }
      if (success)
        break;
}
  • Your bug is here: `(temp[i]='\\') ` this is an assignment not a comparison. I did not initially see that but figured that out with a simple std::cout here: [https://ideone.com/eTz6ld](https://ideone.com/eTz6ld) – drescherjm Feb 02 '21 at 03:07
  • Thank you very much. I would have never caught that on my own. – Ansh Srivastava Feb 02 '21 at 03:08
  • As a side-note, if you are describing an issue with input, and you say that you entered "the date", it's not really that helpful or descriptive. "The date" could be anything. Practice being concise. For example, you could instead provide the exact date that you entered. – paddy Feb 02 '21 at 03:10
  • My advice to find such bugs is to use a cout like I did in that ideone.com link or understand how to use a debugger. Eventually you will need to know how to debug very well if you become a programmer professionally. – drescherjm Feb 02 '21 at 03:10
  • Thanks for the advice. I should have mentioned my exact input. The cout statement was a good idea. It is always a good thing to cross-check even what I think is trivial and easy. Eventually as I get more experienced, I will hopefully learn to question every little thing while debugging. I really should practice using a debugger. Thank you very much for the advice. – Ansh Srivastava Feb 02 '21 at 03:17
  • You may find [std::get_time](https://en.cppreference.com/w/cpp/io/manip/get_time) useful. – Galik Feb 02 '21 at 03:33
  • Thanks for the suggestion. This was actually a project in school that required to get user input otherwise I would have used it. – Ansh Srivastava Feb 02 '21 at 05:36

1 Answers1

0

In your if condition, you are assigning // to temp index i on line 10. A white space is also being assigned on the 11th line to temp index i. I am sure this was just a "typo" but in programming the double equals sign is a comparator operator while one equal sign is an assigning operator.

4N0M41Y
  • 324
  • 1
  • 4
  • 12
  • Thanks. I have changed the code and it works after changing it from = to ==. I intended to use the comparison operator == in line 10. – Ansh Srivastava Feb 02 '21 at 05:35