0

I'm not sure what's wrong

Why doesn't my code check for the last 2 cases in Convert US date to EU date challenge? #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; int main() { string a; getline(cin,a); if(a[a.length()-5]=='/'){ stringstream s(a); vector <string> v; string b; while(getline(s,b,'/')){ v.push_back(b); } cout<<v[1]<<'/'<<v[0]<<'/'<<v[2]; }else{ stringstream s(a); string b,c,d; s>>b>>c>>d; c.erase(c.length()-1); string month1[]= {"January","February","March","April","May","June","July","August","September","October","November","December"}; string month2[]= {"1","2","3","4","5","6","7","8","9","10","11","12"}; for(int i=0; i<12; i++){ if(b==month1[i]){ b=month2[i]; } } cout<<b<<'/'<<c<<'/'<<d; } return 0; }

12th Jul 2024, 12:21 AM
Abbas Maatouk
5 Antworten
+ 4
I tested your code with May 1, 2020. It printed: 5/1/2020 Whereas it should print: 1/5/2020.
12th Jul 2024, 12:31 AM
Brian
Brian - avatar
+ 3
check your cout
12th Jul 2024, 3:34 AM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 2
Abbas Maatouk if you want a shorter version, take advantage of c++'s other libraries. stringstream and iomanip can be pretty convenient. tm is the struct to use when working with date and time. Dig deeper into C++. From the code you wrote, I feel you already have the basics well covered 😎. This is the code I used: #include <iostream> #include <iomanip> using namespace std; int main() { string inpt; getline(cin, inpt); stringstream ss(inpt); tm t{}; if(inpt.find(',')<string::npos) ss >> get_time(&t, "%B %d, %Y"); else ss >> get_time(&t, "%m/%e/%Y"); cout << (t.tm_mday)<<'/'<<(1+t.tm_mon) <<'/'<<(1900+t.tm_year)<<endl; } /* Sample input: --------------------- December 25, 1975 submit expected output: 25/12/1975 --------------------- 3/1/1870 submit expected output: 1/3/2870 */
12th Jul 2024, 2:26 PM
Bob_Li
Bob_Li - avatar
+ 1
Brian's right. you should reverse the day and month at the end of your code cout<<c<<'/'<<b<<'/'<<d;
12th Jul 2024, 1:45 PM
Bob_Li
Bob_Li - avatar
0
Peter mwangi
13th Jul 2024, 7:14 PM
Peter Mwangi lazu