| | |
cin.fail() - why it fails after cin.get(...)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
The crux of the problem, intended for learning purposes, is that after the line
I'm curious or confused as to why the cin.fail() error has occurred since the array
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.
cin.fail() == true . cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char getdata; cout << "Enter one character: "; cin >> getdata; cin.get(); char line[20]; cout << "\nEnter a line of text 20 characters max: "; cin.get(line,20); cout << "\nLine of text entered : " << line; //cin.get(); char line2[20]; cout << "\nEnter another line of text 20 characters max: "; cin.get(line2,20); cout << "\nSecond line of text entered : " << line2; cout << "\n\nNow at the end of the program"; cout << "\nFirst character of line[20] " << (int)line[0] << endl; cout << "First character of line2[20] : " << (int)line2[0] << endl; if (cin.eof()) cout << "\ncin.eof() = true"; else cout << "\ncin.eof() = false"; if (cin.fail()) cout << "\ncin.fail() = true"; else cout << "\ncin.fail() = false"; // exit routine cin.clear(); while (cin.get() != '\n') continue; cin.get(); return 0; }
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.
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)
int main() { char getdata; cout << "Enter one character: "; cin >> getdata; cin.get(); char line[20] = {0}; cout << "\nEnter a line of text 20 characters max: "; cin.get(line,20); cout << "\nLine of text entered : " << line << "\n"; cin.ignore(); char line2[20] = {0}; cout << "\nEnter another line of text 20 characters max: "; cin.get(line2,20); cin.ignore(); cout << "\nSecond line of text entered : " << line2 << "\n"; cout << "\n\nNow at the end of the program"; cout << "\nFirst character of line[20] " << (int)line[0] << endl; cout << "First character of line2[20] : " << (int)line2[0] << endl; if (cin.eof()) cout << "\ncin.eof() = true"; else cout << "\ncin.eof() = false"; if (cin.fail()) cout << "\ncin.fail() = true"; else cout << "\ncin.fail() = false"; // exit routine cin.clear(); while (cin.get() != '\n') continue; cin.get(); return 0; }
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.
If you use getline() instead of get() you don't have to call ignore()
C++ Syntax (Toggle Plain Text)
int main() { char getdata; cout << "Enter one character: "; cin >> getdata; cin.get(); char line[20] = {0}; cout << "\nEnter a line of text 20 characters max: "; cin.getline(line,20); cout << "\nLine of text entered : " << line << "\n"; //cin.ignore(); char line2[20] = {0}; cout << "\nEnter another line of text 20 characters max: "; cin.getline(line2,20); //cin.ignore(); cout << "\nSecond line of text entered : " << line2 << "\n"; cout << "\n\nNow at the end of the program"; cout << "\nFirst character of line[20] " << (int)line[0] << endl; cout << "First character of line2[20] : " << (int)line2[0] << endl; if (cin.eof()) cout << "\ncin.eof() = true"; else cout << "\ncin.eof() = false"; if (cin.fail()) cout << "\ncin.fail() = true"; else cout << "\ncin.fail() = false"; // exit routine cin.clear(); while (cin.get() != '\n') continue; cin.get(); return 0; }
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.
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
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. ![]() |
Similar Threads
- C / C++ FAQ's and Practice problems (C++)
- How do I flush the input stream? (C++)
- cin.reset() (C++)
- Array of words (C++)
- using bool with do while loop (C++)
- I need some help with basic C++ (C++)
- infinite loop... (C++)
- Counting the amount of letters in a phrase! (C++)
- Time display program (C++)
- need help in creating simple line editor (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ code problem
- Next Thread: Decimal Value of the Ratios of Consecutive Fibonacci Numbers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






