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.