Is checking for cin.eof() really necessary?

I have written a simple program that reads character input from the keyboard within a For loop. My professor has taken away 10 points for not checking the input using cin.eof. I don’t understand why this is necessary? Presumably any non-integer character should be acceptable. The prof specifically said not to check for integers.

I ran the program using Visual Studio 10, and entered CTRL-Z as input. The program quit anyway even though I did not specifically code for the EOF condition. Can someone explain why? And is the professor correct insisting to check for cin.eof or is this antiquated style?

Here is an example code snippet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cout << "Enter a list of 10 words" << endl;
for (i= 0;i< MAX; i++) {
getline(cin, input);
Word word(input);
noun_array[nElements]= word;
}
</code>
<code>cout << "Enter a list of 10 words" << endl; for (i= 0;i< MAX; i++) { getline(cin, input); Word word(input); noun_array[nElements]= word; } </code>
cout << "Enter a list of 10 words" << endl;

for (i= 0;i< MAX; i++) { 
    getline(cin, input);
    Word word(input);
    noun_array[nElements]= word;
} 

3

I ran the program using Visual Studio 10, and entered CTRL-Z as input.
The program quit anyway even though I did not specifically code for
the EOF condition. Can someone explain why?

The program doesn’t quit.

The end-of-file character (CTRLZ on the keyboard) sets the internal state flag of std::cin to eofbit, which must be cleared with basic_ios::clear() before following calls to getline will work properly.

The loop is executed MAX times but getline won’t append characters to input.

I’m not sure this is what you want. If it works, it’s probably a matter of luck.

How to check? eof() has some downsides:

Both eof() and feof() check the state of an input stream to see if an
end-of-file condition has occurred. Such a condition can only occur
following an attempted read operation. If you call either function
without previously performing a read, your code is wrong! Never loop
on an eof function.

(From All about EOF)

The EOF state may not get set until after a read is attempted past the end of file. That is, reading the last byte from a file might not set the EOF state.

Moreover

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!std::cin.eof())
</code>
<code>if (!std::cin.eof()) </code>
if (!std::cin.eof())

will test for end-of-file, not for errors.

You should prefer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (getline(std::cin, input))
{
// ...
}
</code>
<code>if (getline(std::cin, input)) { // ... } </code>
if (getline(std::cin, input))
{
  // ...
}

See also C++ FAQ Section 15.5 and How to determine whether it is EOF when using getline() in c++?

EDIT

It should be something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>std::cout << "Enter a list of 10 words" << std::endl;
for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
// ...
// Word word(input);
// noun_array[nElements] = word;
// ...
}
</code>
<code>std::cout << "Enter a list of 10 words" << std::endl; for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i) { // ... // Word word(input); // noun_array[nElements] = word; // ... } </code>
std::cout << "Enter a list of 10 words" << std::endl;

for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
  // ...
  // Word word(input);
  // noun_array[nElements] = word;
  // ...
}

This way the program performs its processing only with a valid input.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is checking for cin.eof() really necessary?

I have written a simple program that reads character input from the keyboard within a For loop. My professor has taken away 10 points for not checking the input using cin.eof. I don’t understand why this is necessary? Presumably any non-integer character should be acceptable. The prof specifically said not to check for integers.

I ran the program using Visual Studio 10, and entered CTRL-Z as input. The program quit anyway even though I did not specifically code for the EOF condition. Can someone explain why? And is the professor correct insisting to check for cin.eof or is this antiquated style?

Here is an example code snippet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cout << "Enter a list of 10 words" << endl;
for (i= 0;i< MAX; i++) {
getline(cin, input);
Word word(input);
noun_array[nElements]= word;
}
</code>
<code>cout << "Enter a list of 10 words" << endl; for (i= 0;i< MAX; i++) { getline(cin, input); Word word(input); noun_array[nElements]= word; } </code>
cout << "Enter a list of 10 words" << endl;

for (i= 0;i< MAX; i++) { 
    getline(cin, input);
    Word word(input);
    noun_array[nElements]= word;
} 

3

I ran the program using Visual Studio 10, and entered CTRL-Z as input.
The program quit anyway even though I did not specifically code for
the EOF condition. Can someone explain why?

The program doesn’t quit.

The end-of-file character (CTRLZ on the keyboard) sets the internal state flag of std::cin to eofbit, which must be cleared with basic_ios::clear() before following calls to getline will work properly.

The loop is executed MAX times but getline won’t append characters to input.

I’m not sure this is what you want. If it works, it’s probably a matter of luck.

How to check? eof() has some downsides:

Both eof() and feof() check the state of an input stream to see if an
end-of-file condition has occurred. Such a condition can only occur
following an attempted read operation. If you call either function
without previously performing a read, your code is wrong! Never loop
on an eof function.

(From All about EOF)

The EOF state may not get set until after a read is attempted past the end of file. That is, reading the last byte from a file might not set the EOF state.

Moreover

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!std::cin.eof())
</code>
<code>if (!std::cin.eof()) </code>
if (!std::cin.eof())

will test for end-of-file, not for errors.

You should prefer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (getline(std::cin, input))
{
// ...
}
</code>
<code>if (getline(std::cin, input)) { // ... } </code>
if (getline(std::cin, input))
{
  // ...
}

See also C++ FAQ Section 15.5 and How to determine whether it is EOF when using getline() in c++?

EDIT

It should be something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>std::cout << "Enter a list of 10 words" << std::endl;
for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
// ...
// Word word(input);
// noun_array[nElements] = word;
// ...
}
</code>
<code>std::cout << "Enter a list of 10 words" << std::endl; for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i) { // ... // Word word(input); // noun_array[nElements] = word; // ... } </code>
std::cout << "Enter a list of 10 words" << std::endl;

for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
  // ...
  // Word word(input);
  // noun_array[nElements] = word;
  // ...
}

This way the program performs its processing only with a valid input.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is checking for cin.eof() really necessary?

I have written a simple program that reads character input from the keyboard within a For loop. My professor has taken away 10 points for not checking the input using cin.eof. I don’t understand why this is necessary? Presumably any non-integer character should be acceptable. The prof specifically said not to check for integers.

I ran the program using Visual Studio 10, and entered CTRL-Z as input. The program quit anyway even though I did not specifically code for the EOF condition. Can someone explain why? And is the professor correct insisting to check for cin.eof or is this antiquated style?

Here is an example code snippet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cout << "Enter a list of 10 words" << endl;
for (i= 0;i< MAX; i++) {
getline(cin, input);
Word word(input);
noun_array[nElements]= word;
}
</code>
<code>cout << "Enter a list of 10 words" << endl; for (i= 0;i< MAX; i++) { getline(cin, input); Word word(input); noun_array[nElements]= word; } </code>
cout << "Enter a list of 10 words" << endl;

for (i= 0;i< MAX; i++) { 
    getline(cin, input);
    Word word(input);
    noun_array[nElements]= word;
} 

3

I ran the program using Visual Studio 10, and entered CTRL-Z as input.
The program quit anyway even though I did not specifically code for
the EOF condition. Can someone explain why?

The program doesn’t quit.

The end-of-file character (CTRLZ on the keyboard) sets the internal state flag of std::cin to eofbit, which must be cleared with basic_ios::clear() before following calls to getline will work properly.

The loop is executed MAX times but getline won’t append characters to input.

I’m not sure this is what you want. If it works, it’s probably a matter of luck.

How to check? eof() has some downsides:

Both eof() and feof() check the state of an input stream to see if an
end-of-file condition has occurred. Such a condition can only occur
following an attempted read operation. If you call either function
without previously performing a read, your code is wrong! Never loop
on an eof function.

(From All about EOF)

The EOF state may not get set until after a read is attempted past the end of file. That is, reading the last byte from a file might not set the EOF state.

Moreover

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (!std::cin.eof())
</code>
<code>if (!std::cin.eof()) </code>
if (!std::cin.eof())

will test for end-of-file, not for errors.

You should prefer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (getline(std::cin, input))
{
// ...
}
</code>
<code>if (getline(std::cin, input)) { // ... } </code>
if (getline(std::cin, input))
{
  // ...
}

See also C++ FAQ Section 15.5 and How to determine whether it is EOF when using getline() in c++?

EDIT

It should be something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>std::cout << "Enter a list of 10 words" << std::endl;
for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
// ...
// Word word(input);
// noun_array[nElements] = word;
// ...
}
</code>
<code>std::cout << "Enter a list of 10 words" << std::endl; for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i) { // ... // Word word(input); // noun_array[nElements] = word; // ... } </code>
std::cout << "Enter a list of 10 words" << std::endl;

for (unsigned i(0); i < MAX && std::getline(std::cin, input); ++i)
{
  // ...
  // Word word(input);
  // noun_array[nElements] = word;
  // ...
}

This way the program performs its processing only with a valid input.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật