cin.fail() - why it fails after cin.get(...)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

cin.fail() - why it fails after cin.get(...)

 
0
  #1
Apr 22nd, 2008
I'm self studying C++ and I'm actually back-tracking a bit in order to clarify things. My question revolves around why in the following code is the cin.fail() == true .

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char getdata;
  6. cout << "Enter one character: ";
  7. cin >> getdata;
  8. cin.get();
  9.  
  10. char line[20];
  11. cout << "\nEnter a line of text 20 characters max: ";
  12. cin.get(line,20);
  13. cout << "\nLine of text entered : " << line;
  14. //cin.get();
  15.  
  16. char line2[20];
  17. cout << "\nEnter another line of text 20 characters max: ";
  18. cin.get(line2,20);
  19. cout << "\nSecond line of text entered : " << line2;
  20.  
  21. cout << "\n\nNow at the end of the program";
  22. cout << "\nFirst character of line[20] " << (int)line[0] << endl;
  23. cout << "First character of line2[20] : " << (int)line2[0] << endl;
  24.  
  25. if (cin.eof())
  26. cout << "\ncin.eof() = true";
  27. else
  28. cout << "\ncin.eof() = false";
  29. if (cin.fail())
  30. cout << "\ncin.fail() = true";
  31. else
  32. cout << "\ncin.fail() = false";
  33.  
  34. // exit routine
  35. cin.clear();
  36. while (cin.get() != '\n')
  37. continue;
  38. cin.get();
  39. return 0;
  40. }
The crux of the problem, intended for learning purposes, is that after the line cin.get(line,20); the newline character remains in the input queue. The very next cin.get(line2,20) , from a users point of view, is ignored or bypassed. The array line2 is in fact assigned the null character as indicated by the cout << "....line2[20]...." statement.

I'm curious or confused as to why the cin.fail() error has occurred since the array line2 has been assigned the null character.

As I've just written this, it's made me think a little. Is it the case that cin.get(array, size) leaves the newline character in the input queue and since the first character is a newline character, it's ignored and therefore nothing to assign to the array variable, hence an error state created. I'll post this question anyway.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,670
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: cin.fail() - why it fails after cin.get(...)

 
0
  #2
Apr 23rd, 2008
try this slightly modified version of your program. In both cases you can only enter 19 characters, not 20, because the 20th byte is reserved for the string's null teriminator -- 0.
  1. int main()
  2. {
  3. char getdata;
  4. cout << "Enter one character: ";
  5. cin >> getdata;
  6. cin.get();
  7.  
  8. char line[20] = {0};
  9. cout << "\nEnter a line of text 20 characters max: ";
  10. cin.get(line,20);
  11. cout << "\nLine of text entered : " << line << "\n";
  12. cin.ignore();
  13.  
  14. char line2[20] = {0};
  15. cout << "\nEnter another line of text 20 characters max: ";
  16. cin.get(line2,20);
  17. cin.ignore();
  18. cout << "\nSecond line of text entered : " << line2 << "\n";
  19.  
  20. cout << "\n\nNow at the end of the program";
  21. cout << "\nFirst character of line[20] " << (int)line[0] << endl;
  22. cout << "First character of line2[20] : " << (int)line2[0] << endl;
  23.  
  24. if (cin.eof())
  25. cout << "\ncin.eof() = true";
  26. else
  27. cout << "\ncin.eof() = false";
  28. if (cin.fail())
  29. cout << "\ncin.fail() = true";
  30. else
  31. cout << "\ncin.fail() = false";
  32.  
  33. // exit routine
  34. cin.clear();
  35. while (cin.get() != '\n')
  36. continue;
  37. cin.get();
  38. return 0;
  39. }
Last edited by Ancient Dragon; Apr 23rd, 2008 at 12:42 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,670
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: cin.fail() - why it fails after cin.get(...)

 
0
  #3
Apr 23rd, 2008
If you use getline() instead of get() you don't have to call ignore()
  1. int main()
  2. {
  3. char getdata;
  4. cout << "Enter one character: ";
  5. cin >> getdata;
  6. cin.get();
  7.  
  8. char line[20] = {0};
  9. cout << "\nEnter a line of text 20 characters max: ";
  10. cin.getline(line,20);
  11. cout << "\nLine of text entered : " << line << "\n";
  12. //cin.ignore();
  13.  
  14. char line2[20] = {0};
  15. cout << "\nEnter another line of text 20 characters max: ";
  16. cin.getline(line2,20);
  17. //cin.ignore();
  18. cout << "\nSecond line of text entered : " << line2 << "\n";
  19.  
  20. cout << "\n\nNow at the end of the program";
  21. cout << "\nFirst character of line[20] " << (int)line[0] << endl;
  22. cout << "First character of line2[20] : " << (int)line2[0] << endl;
  23.  
  24. if (cin.eof())
  25. cout << "\ncin.eof() = true";
  26. else
  27. cout << "\ncin.eof() = false";
  28. if (cin.fail())
  29. cout << "\ncin.fail() = true";
  30. else
  31. cout << "\ncin.fail() = false";
  32.  
  33. // exit routine
  34. cin.clear();
  35. while (cin.get() != '\n')
  36. continue;
  37. cin.get();
  38. return 0;
  39. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: cin.fail() - why it fails after cin.get(...)

 
0
  #4
Apr 23rd, 2008
Thanks AD. I'm in the process of coming to grips with all or most of the basic cin uses and I was more concerned with why an error state is recorded when there is a newline left in the input buffer and then an immediate cin.get(arrayname,20) is issued.

Anyway, I've never used the ignore method before, thanks for the clue there. In lieu of the ignore function I've been using cin.get() to clear out errant data.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2952 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC