0

So i have this 3 functions:

void bookings(const std::string name, const Date& date)const;
void bookings(const std::string name)const;
void bookings(const Date& date)const;

I want to use them in 3 different ways. First, if i pass the name and the date to use the first function. Second if i pass only the name to use the second function and if i pass only the date to use the third function. I am using the console to pass the arguments. Is there any way to do that? I write this :

std::string event, name;
        int d, m, y;
        std::cout << "event: ";
        std::getline(std::cin, event);
        std::cout << "date: ";
        std::cin >> d >> m >> y;
        if (event == "")
        {
            bookings(Date(d, m, y));
        }
        else if (d > 0 && m>0 && y>0) 
        {
            bookings(event, Date(d, m, y));
        }
        else
        {
            bookings(event);
        }

but in the case when i don't pass the date doesn't work. I know it is something with the arguments of the date and that in this case they aren't defined. Is there any way to fix this?

Dontor
  • 11
  • 1
  • wouldn't it be easier to always call `bookings(event, Date(d, m, y));` and let the method check if event is empty or it is a valid date? – 463035818_is_not_an_ai May 01 '20 at 12:22
  • please epxplain what is "doesn't work" do you get a compiler error? wrong output? – 463035818_is_not_an_ai May 01 '20 at 12:22
  • also read https://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin, maybe that is the problem – 463035818_is_not_an_ai May 01 '20 at 12:23
  • no there is not a comiler error. I just want when i don't pass any arguments for the date to call the last function, but when i skip passing the date arguments the program doesn't continue until i pass them. – Dontor May 01 '20 at 12:27
  • what you mean with "skip passing the date arguments" ? The user has to provide the input. And make sure to read the question I linked above about the problem with getline after cin – 463035818_is_not_an_ai May 01 '20 at 12:30
  • What, exactly, is going wrong? What does the code do, and what do you want it to do? As written, the first call to `bookings` will call `bookings(const Date&)`, the second call to `bookings` will call `bookings(const std::string, const Date&)`, and the third call to `bookings` will call `bookings(const std::string)`. You are not "using the console to pass the arguments". The code uses the console to get values for the arguments, and the code passes those values to `bookings`. – Pete Becker May 01 '20 at 13:33

1 Answers1

0

You can get your date as strings using getline than check if they are set or not:

    std::string event, name;
    std::string sD, sM, sY;

    int d, m, y;
    d = m = y = 0;

    std::cout << "event: ";
    std::getline(std::cin, event);

    std::cout << "date: ";
    std::getline(std::cin, sD);
    std::getline(std::cin, sM);
    std::getline(std::cin, sY);

    if(!sD.empty() && !sM.empty() && !sY.empty())
    {
        d = atoi(sD.c_str());
        m = atoi(sM.c_str());
        y = atoi(sY.c_str());
    }

    if (event == "")
    {
        bookings(Date(d, m, y));
    }
    else if (d > 0 && m>0 && y>0) 
    {
        bookings(event, Date(d, m, y));
    }
    else
    {
        bookings(event);
    }
SeventhSon84
  • 364
  • 1
  • 4