0
//C++ program two find number of days between two given dates
    #include "stdafx.h"
    #include<iostream>
    #include<string>

    using namespace std;

        // A date has day 'd', month 'm' and year 'y' 
    // 'h' hour , 'min' minute ; 'sec' second
    struct Date
    {
        int d, m, y, h, min, sec;
    };

Problem is in main :

int main(int argc, char** argv)
{
    string line;
    getline(cin, line);

    int test = stoi(line);
    unsigned int * tab = new unsigned int[test];
    for (int i = 0; i <test; i++)
        tab[i] = 0;
    for (int i = 0; i<test; i++)
    {
        getline(cin, line);
        int n1 = stoi(line);                     
        int n2 = stoi(line);
        tab[i] = getDifference(n1, n2);  // HERE IS PROBLEM 
    }   

    for (int i = 0; i<test; i++)
            {
                cout << tab[i] << endl;
            }

The error pops up: No proper constructor to redirect "int" to "Date"

How to solve it? My task is to download data through CMD, the first line is the number of tests, the next is 5 sets of dates to count the difference in days between them. Do you know how to tell the program that the first two lines are one set?

Lynn
  • 10,425
  • 43
  • 75
Ula
  • 3
  • 1
  • 1
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here. It's especially important to post a [mcve]. – R Sahu May 31 '18 at 17:52
  • You should search the internet for examples, using search terms as "C++ find days between dates". I very sure there are already similar questions on StackOverflow. – Thomas Matthews May 31 '18 at 18:11
  • Possible duplicate of [Number of days between two dates C++](https://stackoverflow.com/questions/14218894/number-of-days-between-two-dates-c) – modesitt May 31 '18 at 18:45
  • Already wrote, I have only a problem with the constructor of redirecting the element "int" to "date" – Ula May 31 '18 at 19:03

1 Answers1

0

You'll need to read in each number separately into the members of a Date-object, and then compare Date-objects, not just single integers. Strategy is to read in complete lines (e.g. "20 10 2017") and then use a stringstream for reading in the separate numbers:

#include <sstream>

struct Date
{
    int d, m, y, h, min, sec;
};

ostream& operator << (ostream& o, Date & d) {
    o << d.d << "/" << d.m << "/" << d.y;
    return o;
};

int main() {

    std::string line;
    if (std::getline(std::cin,line)) {
        int nrOfSets = 0;
        stringstream reader(line);
        reader >> nrOfSets;
        int i=0;
        Date previousDate;
        while (i < nrOfSets && getline(cin,line)) {
            Date d;
            reader = stringstream(line);
            if (! (reader >> d.d >> d.m >> d.y)) {
                cout << "invalid input." << endl;
                continue;
            }
            i++;
            if (i > 1) { // already two dates entered?
                cout << "calculating difference between " << d << " and " << previousDate << ":" << endl;
                // your code: int difference = calcDifference(d, previousDate);
            }
            previousDate = d;
        }
    }
}

Input / Output:

2
20 10 2017
22 10 2017
calculating difference between 22/10/2017 and 20/10/2017:
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • The program is great, but there was a problem when data was uploaded by CMD (file.exe is not recognized as an interial or external command, operable program or batch file. – Ula May 31 '18 at 19:07
  • How do you "build" your program, do you compile *and* link it? are you using an IDE, or are you calling `gcc` on your own? – Stephan Lechner May 31 '18 at 19:09
  • For input data: 5 1997 12 31 0 0 0 1998 1 1 0 0 0 1997 12 31 23 59 59 1998 12 31 23 59 59 1997 12 31 23 59 59 1998 1 1 23 59 58 2000 2 20 0 0 0 2000 3 20 23 59 59 2000 2 29 0 0 0 2000 3 1 0 0 0 the correct result is: 1 365 0 29 1 – Ula May 31 '18 at 19:20