Using a string in a calculation with arrays

Hi

My task is to accept user input for a date in the format "dd mm" and then use the day and month to do calculation with array.

The julian format should be calculated i.e the day of the year the 'dd mm' represents which is how many days has passed since "01 01".

The array is int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; So if '27 03' is entered then the julian format of that date is 31 + 28 + 27 = 86.

I am having trouble with using the string as input by the user in the int array calculation. I am not even sure if my method is possible. Wondering if there is some kind of conversion function or typecasting method. My attempted below.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
  #include <string>
  using namespace std;


int main()
{
    int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    string date1, date2, dayDate1, monthDate1, dayDate2, monthDate2,        julianDate1, julianDate2;

    cout << "Enter the first date (in the format of '27 03') : ";
    getline (cin, date1);
    cout << "Enter the second date (in the format of '27 03') : ";
    getline (cin, date2);

    dayDate1 = date1.substr(0, 2);
    monthDate1 = date1.substr(3, 2);
    dayDate2 = date2.substr(0, 2);
    monthDate2 = date2.substr(0, 2);

    int dayDate1Int = dayDate1;

    /*for ( monthDate2 > 0; monthDate2 --)
       {
          julianDate1 += daysPerMonth[monthDate1];
        
       }*/
     //finalJulianDate1 = julianDate1 + dayDate1;
    //cout << monthDate1;



    return 0;


}
use std::stoi to change strings into int.
Line 20: should be substr(3,2);

After you convert make a loop and iterate through the elements of the daysPerMonth array and add it into an integer variable.
If you don't have to use std::string, it'd be much easier to just use ints.
1
2
3
4
5
6
7
8
9
const int num_dates = 2;

int dayDate[num_dates] = { 0 }, monthDate[num_dates] = { 0 };
for( int i = 0; i < num_dates; i++ ) {
    cout << "Enter date #" << i + 1 << " (in the format of '27 03') : ";
    cin >> dayDate[i] >> monthDate[i];

    // validate input
}


Also, make daysPerMonth const, as their values do not change and to prevent you from accidentally changing them.
std::stoi would not compile. Im guessing not compatible with my compiler? I dont know.

I then used different method to convert string to int, using sstream header.

Should have only ints, would have saved me a lot of time.

So i came up with an implementation that's not the most elegant but it at least it works for the most part.

Thanks so much for the feeback guys.

#include <iostream>
#include <string>
#include<sstream>
#include <cmath>
#include <cstdlib>
#include <climits>

using namespace std;


int main()
{
const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

string date1, date2, dayDateString1, monthDateString1, dayDateString2, monthDateString2;

int monthDateInt1, dayDateInt1, dayDateInt2, monthDateInt2, julianDate1, julianDate2, finalJulianDate1, finalJulianDate2, daysPassed, daysCalc;
julianDate1 = 0;
julianDate2 = 0;

cout << "Enter the first date (in the format of 'dd mm') : ";
getline (cin, date1);
cout << "Enter the second date (in the format of 'dd mm') : ";
getline (cin, date2);

dayDateString1 = date1.substr(0, 2);
monthDateString1 = date1.substr(3, 2);
dayDateString2 = date2.substr(0, 2);
monthDateString2 = date2.substr(3, 2);

if ( ! (istringstream(dayDateString1) >> dayDateInt1) ) dayDateInt1 = 0;
if ( ! (istringstream(monthDateString1) >> monthDateInt1) ) monthDateInt1 = 0;
if ( ! (istringstream(dayDateString2) >> dayDateInt2) ) dayDateInt2 = 0;
if ( ! (istringstream(monthDateString2) >> monthDateInt2) ) monthDateInt2 = 0;

for (int i = monthDateInt1 - 1; i > 0 ; i--)
{
julianDate1 += daysPerMonth[i];
}

for (int i = monthDateInt2 - 1; i > 0 ; i--)
{
julianDate2 += daysPerMonth[i];
}

finalJulianDate1 = julianDate1 + dayDateInt1;
finalJulianDate2 = julianDate2 + dayDateInt2;
daysCalc = (finalJulianDate1 - finalJulianDate2);



cout << endl << "Juian format of date 1 is " << finalJulianDate1 << endl << endl;
cout << "Juian format of date 2 is " << finalJulianDate2 << endl << endl;
cout << "Days passed between the two dates are " << std::abs(daysCalc) << endl;

return 0;


}
1
2
3
4
5
6
7
8
9
dayDateString1 = date1.substr(0, 2);
monthDateString1 = date1.substr(3, 2);
dayDateString2 = date2.substr(0, 2);
monthDateString2 = date2.substr(3, 2);

if ( ! (istringstream(dayDateString1) >> dayDateInt1) ) dayDateInt1 = 0;
if ( ! (istringstream(monthDateString1) >> monthDateInt1) ) monthDateInt1 = 0;
if ( ! (istringstream(dayDateString2) >> dayDateInt2) ) dayDateInt2 = 0;
if ( ! (istringstream(monthDateString2) >> monthDateInt2) ) monthDateInt2 = 0;

You can simplify this code by passing the date strings directly to istringstream without using substr.

1
2
istringstream(date1) >> dayDateInt1 >> monthDateInt1;
istringstream(date2) >> dayDateInt2 >> monthDateInt2;


If you're using C++11 or later the >> operator will automatically set the int variables to zero if it fails to read the value.

This way is also more robust because it will work even if the user inputs only one digit for the day or put more than one space between the values.
Topic archived. No new replies allowed.