Thanks in advance for reading. I'm new to these boards so I hope it's alright that my first post is a question.
Generally I use cin.get(); to pause a console application right before it ends, so that I can see what I've done without using cmd.exe to exec my program.
Today though, I tried my hand at classes, and for some reason if I don't cin.ignore(); after every input instance in the program, the program runs and then quits, 'ignoring' my cin.get(); at the end.
The program is posted below with the cin.ignore();'s commented out. I was just wondering if anyone can tell me WHY I need cin.ignore(); just because a class exists somewhere in the program? Is there a better way to do it?
Code:
#include <iostream>
using namespace std;
class DayOfYear
{
public:
void output();
int month;
int day;
};
int main(void)
{
DayOfYear today, birthday;
cout << "Today's Date:\n\n"
<< "Please enter the current month as a number: ";
cin >> today.month;
// cin.ignore();
cout << "And the day. What day is it? ";
cin >> today.day;
// cin.ignore();
cout << "Birthdate:\n\n"
<< "Enter your birthmonth as a number: ";
cin >> birthday.month;
// cin.ignore();
cout << "And last, enter the day you were born: ";
cin >> birthday.day;
// cin.ignore();
cout << "Today's date is ";
today.output();
cout << "Your birthday is ";
birthday.output();
if (today.month == birthday.month && today.day == birthday.day)
cout << "Happy Birthday!\n";
else cout << "Happy Unbirthday!\n";
cin.get();
return 0;
}
void DayOfYear::output()
{
cout << "month = " << month << ", day = " << day << endl;
}
PS: I'm using Visual C++ .net 2003 Standard, creating a new blank and empty win32 console application and right clicking "source files" and choosing "add>add new item" which is a .cpp file.
EDIT: I just built it with the ignores commented out, but at the end a cin.ignore(); after the final cin.get(); and it has the same effect (the program pauses). So now I'm more confused than ever.