-1

I made a program that let's the user type in a date, if that date isn't booked then it will print out "Booked" in swedish. If that date already is booked it will type out "Already booked" in swedish. The program will continue to ask the user to type in a date until the user terminates the program using Ctrl-D.

So in english the terminal should look like:

Enter date: 2021-04-15

Booking made!

Enter date: 4040-03-10

Booking made!

Enter date: 2021-04-15

Can not make reservation, this date is already booked!

Enter date: 6543-11-28

Booking made!

Enter a date:

[ctrl-d]

However in my program when I press Ctrl-D it end but it will print out "Can not make reservation, this date is already booked!" (in swedish of course). I only want it to end without it printing out anything.

#include <iostream>
#include <vector>

using namespace std;

struct Date_Type {
    int year;
    int month;
    int day;
};

void get(Date_Type& date)
{
    cout << "Mata in datum: ";

    cin >> date.year;

    cin.ignore(1);

    cin >> date.month;

    cin.ignore(1);

    cin >> date.day;
}

void add(vector<Date_Type>& dates, Date_Type& date)
{

    bool check_date = false;

    for (int i = 0; i < dates.size(); i++) {

        if (date.year == dates[i].year && date.month == dates[i].month && date.day == dates[i].day) {

            check_date = true;
        }
    }

    if (check_date == false) {

        dates.push_back(date);

        cout << "Bokning gjord!" << endl;
    }
    else

        cout << "Kan ej göra bokning, detta datum är redan bokat!" << endl;
}

int main()
{
    Date_Type date{};

    vector<Date_Type> dates{};

    while (cin)

    {

        get(date);

        add(dates, date);
    }

    return 0;
}

I can only think about one thing, and that is that it somehow it is stored in the buffert and I need to do a cin.ignore but I have absolutely no clue where I shall do it. Why does my program act that way and what is the possible fix.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
Cidrant
  • 7
  • 2

1 Answers1

0

When you press Ctrl+D the cin sets its end-of-file flag, and all of the following inputs will fail. You don't test for that, so it just runs right through without inputting anything.

That probably leaves the date object unchanged, so you test the previous date once more. Of course that looks like a duplicate.

I would add a "Type 0 to terminate" to the prompt, and then test for that input and terminate properly.

BoP
  • 2,310
  • 1
  • 15
  • 24
  • Control-D actually just transmits data in the terminal buffer to the attached stream. If there is no data, then none is sent. The stream software interprets a receipt of no data as a (possibly temporary) end-of-file. It is is not pressing control-D that sets the end-of-file flag; that is an artifact of the stream software. In fact, if you type a few characters and press control-D, those characters will be sent to the stream immediately, and the end-of-file flag will not be set. It is only when you press control-D with no pending characters that an end-of-file is simulated. – Eric Postpischil Jan 09 '22 at 15:38
  • *It is only when you press control-D with no pending characters that an end-of-file is simulated.* And that is exactly what happens here... – BoP Jan 09 '22 at 20:56
  • Yes, in this situation, but if you just tell people that control-D triggers end-of-file, they do not learn what it really does, may be surprised when a situation arises in which it does not trigger end-of-file, and may not be aware of the true effect when they need it. – Eric Postpischil Jan 09 '22 at 21:18