0

Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. Any date not following that format is incorrect and should be ignored. Use the substr() function to parse the string and extract the date. The input ends with -1 on a line alone. Output each correct date as: 3-1-1990.

Ex: If the input is:

March 1, 1990

April 2 1995

7/15/20

December 13, 2003

-1

then the output is:

3-1-1990

12-13-2003

code below

#include <iostream>
#include <string>

using namespace std;

int DateParser(string month) {
    int monthInt = 0;
    
    if (month == "January")
        monthInt = 1;
    else if (month == "February")
        monthInt = 2;
    else if (month == "March")
        monthInt = 3;
    else if (month == "April")
        monthInt = 4;
    else if (month == "May")
        monthInt = 5;
    else if (month == "June")
        monthInt = 6;
    else if (month == "July")
        monthInt = 7;
    else if (month == "August")
        monthInt = 8;
    else if (month == "September")
        monthInt = 9;
    else if (month == "October")
        monthInt = 10;
    else if (month == "November")
        monthInt = 11;
    else if (month == "December")
        monthInt = 12;
    return monthInt;
}

int main () {
    string month, date, year, day, validDates[5];
    int pos1, pos2, monthNumber;
    int i = 0;
    string hy = "-";
    
    while(true){
       getline(cin, date);
       if(date == "-1"){
       break; 
       } 
       //March 1, 1990 layout 1
       //April 2 1995 layout 2
       pos1 = date.find(' ');
       pos2 = date.find(',');
       
       month = date.substr(0, pos1);
       //cout << month << endl; tester
       
       day = date.substr(pos1 +1 , pos2-6);
       //cout << day << endl; tester
       
       year = date.substr(date.length()-4, date.length());
      //cout << year << endl; tester
      
     monthNumber = DateParser(month);
     
     if (date.length() - 6 == ',' && monthNumber > 0){
       validDates[++i] = month + hy + day + hy + year;
    }
    }   
    
    for (int j = 0; j < i; j++)
    cout << validDates[j] << endl;
   
}

``

Use the provided DateParser() function to convert a month string to an integer. If the month string is valid, an integer in the range 1 to 12 inclusive is returned, otherwise 0 is returned. Ex: DateParser("February") returns 2 and DateParser("7/15/20") returns 0.

My program is not outputting anything but I believe it has to do with the dates not being put into the array correctly. Please spare me if its obvious.

If anyone could point out the error in my code that would perfect!

  • I see no immediate problems with the code you've shown, but there's also nothing in the code you've shown that would print to the screen. How are you calling this from `main`? – Silvio Mayolo Feb 20 '23 at 22:42
  • @SilvioMayolo Through the for loop that printed the position of the array corresponding to the loop j value. – Gabriel Zuniga Feb 20 '23 at 22:44
  • Is this managed C++? If so, I recommend a DateTime type for easy parsing and display. – Alex T. Feb 20 '23 at 22:54
  • Indeed @AlexT. The IDE is provided through zybooks if that helps. – Gabriel Zuniga Feb 20 '23 at 22:55
  • As @SilvioMayolo pointed out: the code you shared prints nothing. Maybe you should show your complete code (or, even better, a minimal working example), so someone might be able to point out the problem. – kaba Feb 20 '23 at 23:01
  • @kaba this is my complete code. – Gabriel Zuniga Feb 20 '23 at 23:04
  • @kaba for (int j = 0; j < i; j++) cout << validDates[j] << endl; towards the bottom of main is my method of printing. Is this the issue? – Gabriel Zuniga Feb 20 '23 at 23:06
  • @GabrielZuniga Pls forgive me: I didn't recognise the scroll bar, so I didn't see your main function. Without analysing it completely: you use the values that are returned by std::string::find unconditionally. If your char isn't in the date string, you'll end up using npos in later operations which might lead to out_of_range exceptions. So you should test your values pos1 and pos2 before you use them in any operations. – kaba Feb 20 '23 at 23:24
  • @kaba pos1 and pos2 are working accordingly when parsing my strings into the corresponding variable. Hints the testers under each variable. Ive checked and they all work. – Gabriel Zuniga Feb 20 '23 at 23:32
  • But there is no ',' in "April 2 1995" - what would be the value of pos2 in this case? – kaba Feb 20 '23 at 23:43
  • In addition to what's already been mentioned, please consider what `date.length()` is and what you are comparing it to in your last `if` statement. – David Yockey Feb 20 '23 at 23:58
  • Parsing all known date formats is a formidable problem. Please use a known library to do this. https://xkcd.com/1179/ https://stackoverflow.com/q/19482378/14065 – Martin York Feb 21 '23 at 00:13
  • @kaba Oof, me neither. I've never seen a vertical scroll bar on an SO post before. Is that a new thing? – Silvio Mayolo Feb 21 '23 at 01:15

0 Answers0