'How is Ctrl + Z interpreted in the Windows terminal?
I ran the program given below. I am aware of the recommendation to use while (std::getline(std::cin, inp)) instead of while (!std::cin.eof()).
#include <iostream>
#include <string>
int main() {
while (!std::cin.eof()) {
std::string next_line;
std::getline(std::cin, next_line);
if (!std::cin) {
std::cout << "failbit has been set";
return 0;
}
if (next_line.empty()) {
std::cout << "input line was empty";
return 0;
}
}
std::cout << "Success!";
}
Below I give some example input and the program response. ^Z indicates that I typed Ctrl + Z.
In this first example, I typed Ctrl + Z twice across two lines and saw the expected behavior of reaching EOF without flipping the failbit.
Input 1:
this is
my input
and this is the
end^Z
^Z
Output 1:
Success!
In this second example, I was able to add two extra newlines at the end of my input after typing a single Ctrl + Z, however, the eofbit was not toggled.
Input 2:
this is
my input
and this is the
end^Z
Output 2:
input line was empty
In this third example, I saw the expected output of the failbit of std::cin being set. My understanding is that if Ctrl + Z is the EOF, std::getline should set the eofbit of std::cin, but would not yet set the failbit. This is based on line 2 (a) here. The "failbit has been set" output should not be given under normal inputs.
Input 3:
this is
my input
and this is the
end
^Z
Output 3:
failbit has been set
What is Ctrl + Z doing in each of these examples?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
