943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4266
  • C++ RSS
Apr 22nd, 2008
0

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

Expand Post »
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 .

cpp Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Apr 23rd, 2008
0

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

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Apr 23rd, 2008
0

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

If you use getline() instead of get() you don't have to call ignore()
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Apr 23rd, 2008
0

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

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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ code problem
Next Thread in C++ Forum Timeline: Decimal Value of the Ratios of Consecutive Fibonacci Numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC