0

Days between dates challenge - last test case fails.

Ok, so this piece of code is a little messy maybe but it works in almost all the cases, except the last one. I cannot figure out why. Who can help me? Thanks in advance :) /* Unfortunately, the last TEST CASE does not work. By the way, any cool hints on how I could improve the code is highly appreciated. I know it seems a bit 'messy' or 'wordy'... */ #include <algorithm> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> #include <cmath> //function to check leap year bool leap(const int &year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } //check days between the two dates - careful: leap years!! int main() { //create map of months with int std::map<std::string, size_t> map{{"January", 1}, {"February", 2}, {"March", 3}, {"April", 4}, {"May", 5}, {"June", 6}, {"July", 7}, {"August", 8}, {"September", 9}, {"October", 10}, {"November", 11}, {"December", 12}}; std::vector<int> daysInMonths{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //input for example purposes // std::string str1 = "September 14, 1975"; // std::string str2 = "October 20, 2008"; std::string str1 = "July 14, 1985"; std::string str2 = "December 15, 2008"; //code challenge // std::string str1; // std::getline(std::cin, str1); // std::string str2; // std::getline(std::cin, str2); //Output = 1209- std::vector<int> vec1; std::vector<int> vec2; std::stringstream ss1(str1); std::stringstream ss2(str2); std::string te

6th Jan 2023, 7:50 PM
The_Fox
The_Fox - avatar
4 ответов
+ 1
Smith Welder Thanks. This is a good idea too but it completely changes my idea ;)
8th Jan 2023, 9:41 PM
The_Fox
The_Fox - avatar
+ 1
https://en.wikipedia.org/wiki/Julian_day formulas for this have long been invented.. 👍 P. s. I just showed for you how it to do. When you programming something, use algorithms and formulas. Keep learning..
9th Jan 2023, 6:43 AM
Smith Welder
Smith Welder - avatar
+ 1
Smith Welder Ok. Will do next time ;) I just wanted to implement my own approach and make it work.
9th Jan 2023, 4:47 PM
The_Fox
The_Fox - avatar
0
https://code.sololearn.com/cD2TpE4C1Lf0/?ref=app There is two ways how to do it👇
8th Jan 2023, 12:42 PM
Smith Welder
Smith Welder - avatar