![]() |
| | #1 |
| Registered User Join Date: Jul 2005
Posts: 12
| Confused about cin.get(); and classes. 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;
}
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. Last edited by RaccoonKing; 07-17-2005 at 09:00 AM. |
| RaccoonKing is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,275
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| >tell me WHY I need cin.ignore(); just because a class exists somewhere in the program? It has nothing to do with the class. Formatted input and unformatted input don't play well together, so unless you use cin's >> operator exclusively, you'll have issues where there's a newline character left in the stream. cin.get() doesn't do a blocking read if it immediately finds a newline. This exhibits the same problem: Code: #include <iostream>
int main()
{
int x;
std::cout<<"Enter a number: ";
std::cin>> x;
std::cout<<"Press enter to continue";
std::cin.get();
std::cout<<"You pressed enter\n";
}
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #4 |
| Registered User Join Date: Jul 2005
Posts: 12
| Ok, so because cin >> var ends at the first whitespace (which would be the newline I guess?), but does NOT actually use the newline that you give it by pressing [Enter], the newline stays in the buffer and cin.get(); somehow evaluates to "skip me". Any idea why, when I do this: Code: cin.get(); cin.ignore(); return 0; } |
| RaccoonKing is offline | |
| | #5 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| >somehow evaluates to "skip me". You could say it like that, but in reality it doesn't skip anything, it does exactly what you told it to do. cin.get() expects a character. If the stream is not empty, it reads whatever the first character is there and terminates successfully. >at the end, the pause comes back cin.ignore() is an unformatted input function too. Like cin.get(), if the stream is empty when you call it, it waits until there are characters to read.
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #6 |
| Registered User Join Date: Jul 2005
Posts: 12
| Ok, so by putting it at the end cin.get(); eats the buffer and cin.ignore(); is what actually does the waiting. Thanks very much to all of you. |
| RaccoonKing is offline | |
| | #7 |
| Code Goddess Join Date: Sep 2001
Posts: 9,664
| >so by putting it at the end cin.get(); eats the buffer and cin.ignore(); is what actually does the waiting. Yes, though if the stream contains more than one character, it won't work no matter which way you order the two. That's why cin.ignore() is usually first, but the two argument version is used instead to actually clear the stream of all characters, then do a blocking read with cin.get(): Code: #include <limits> //... std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); std::cin.get();
__________________ My best code is written with the delete key. |
| Prelude is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| switches and classes... so very very confused :( plz clarify | MegaManZZ | C++ Programming | 15 | 08-28-2008 01:10 PM |
| Confused about classes | augie0216 | C# Programming | 1 | 02-25-2006 09:08 AM |
| Exporting VC++ classes for use with VB | Helix | Windows Programming | 2 | 12-29-2003 05:38 PM |
| include question | Wanted420 | C++ Programming | 8 | 10-17-2003 03:49 AM |
| A little confused on classes | Munkey01 | C++ Programming | 18 | 02-04-2003 05:59 AM |