Hey there, so when I'm trying to get a value from my array using a variable, it prints out 0 no matter the variable value, although when I put a value in the array and try to get the element value from the array, it prints out just fine.
Edit:
I'm thinking it has something to do with the tuple.
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <tuple>
usingnamespace std;
int dates[3];
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
string userInput1, userInput2;
tuple <int, int, int> splitString(string userInput){
vector<int> vect;
stringstream ss(userInput);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == '/')
ss.ignore();
}
for (i=0; i< vect.size(); i++){
dates[0] = vect.at(0);
dates[1] = vect.at(1);
dates[2] = vect.at(2);
}
return make_tuple(dates[0], dates[1], dates[2]);
}
int calcAge(){
auto [day1, month1, year1] = splitString(userInput1);
auto [day2, month2, year2] = splitString(userInput2);
int days_delta = abs(day2-day1);
int months_delta = abs((month2-month1) * (months[month2-month1]));
int years_delta = abs((year2-year1) * 365);
return abs(days_delta + (years_delta - months_delta));
}
int main (){
cout<<"Enter the first date in the following format 'dd/mm/yyyy': ";
cin >> userInput1;
cout << "Enter the second date in the following format 'dd/mm/yyyy': ";
cin >> userInput2;
cout << "You are " << calcAge() << " days old" << endl;
return 0;
}
@Genado is that something you just tested or are you assuming you'll get -1? Cause I'm using abs to convert the negative numbers into positive numbers.
If month1 > month2, month2 - month1 will be negative.
If I was born in last December (12), and it is February (2), then I'm 2 months old. Not -10 or 10 or something like that.
You're also converting months to days incorrectly, you're just multiplying by the month at the index of month2 - month1, not accounting for differences in days between those two months. (Or leap years, if you care about those)
Dates (and names, languages, cultures) are really annoying for programmers to work with, so you're not alone...
_____________________________________
When you are testing your program for correctness, start with the simplest test cases first.
For example, first just differ the dates by 1 day, then 1 month, then 1 year, then do a combination of year/month/day, then try more complicated tests.
Next time, please don't update your previous posts (replacing code), just post the new code below. It makes it harder for people to follow along.
If I'm using abs, it returns a positive value of it, I just updated my code above.
Not exactly, you're still just doing months[month2-month1], regardless of any outer use of abs.
But I'm still wondering why "months[month2-month1]" returns a value of 0 ?
Beause if months2-month1 is a value < 0 or >= 12, you engender undefined behavior in your program, due to an out-of-bounds array index. The result is undefined.
Another test case you should look into:
cplusplus249261>main
Enter the first date in the following format 'dd/mm/yyyy': 31/12/1991
Enter the second date in the following format 'dd/mm/yyyy': 01/01/1992
You are 395 days old
emilo0212,
I don't think that there is anywhere in this problem that taking the difference month2-month1 is going to help you. Months unfortunately have different numbers of days. Work with cumulative days since start of year (which will also help when you come to recognise leap years). Once you are down to years and days you should be able to use normal subtraction, "carrying" if necessary.
You don't come from a python background, do you? We don't see so many occurrences of tuples here, although we do regularly see pythonesque idioms, particularly feeding into misuse of the comma operator.
It might also help to think about a secondary problem: Given a date, what is the next date if I increment the day by 1? e.g. 31/12/1991 --> 01/01/1992, like in my other exampe.