简体   繁体   中英

Why does this program work?

I recently wrote a program that takes inputted char data, tests if it is acceptable (az, # marks the end of the input) and puts it in a stack which then tests to see if it's a palindrome. I was expecting to enter it one char by a time, but if I enter a string ended by pound it.. works. Here is some of the relevant code:

char buffer;
bool pound_test = false;
bool palindrome = false;
bool keep_going = true;
stack<char> stack1, stack2, stack3;
string str = "";

cout << "Please enter a string, then end it with the pound sign. " << endl;

while(pound_test == false) {
    cin >> buffer;

    if((buffer >= 97) && (buffer <= 122))
    {
        stack1.push(buffer);
        stack2.push(buffer);
        str += buffer;
    }

    if((buffer >= 65) && (buffer <= 90)) {
        buffer = buffer + 32;
        stack1.push(buffer);
        stack2.push(buffer);
        str += buffer;
    }

    if(buffer == '#')
        pound_test = true;
}

So, when the user enters one long string, like "racecar#" and presses enter, the program properly puts it into the stack. My question is simply: why? Wouldn't the data have to be inputted one char at a time for it to work properly, because the cin is in the loop itself, and the loop has to repeat to enter multiple chars into the stack, right? Thanks!

Edit: Thanks for the answers/comments everyone! I'm really impressed by the quick and kind replies. I'm certainty going to use this site again.

Console input (via the cin std::istream object) in most systems is line buffered. So when you call cin::operator>> for a single character, the function does not in fact return until you press newline (because the underlying I/O system does not make data available to cin until then). Any data entered up-to and including the <newline> will be buffered and subsequent calls to cin::operator>> will be serviced from the buffer until it is exhausted.

In this case cin >> buffer , where buffer is of type char will indeed get a single character, but before that the console buffered an entire line and will use it to satisfy subsequent console input operations.

If you step through your code in your debugger the operation may be clearer to you.

“系统”(操作系统,库,无论什么 - 取决于实现)吃了来自输入的数据字符串,但是你的程序用char读取它。

While all the answers about os buffering are true, I think the confusion can be traced to cin 's operator >> (char) ; because C++ can overload methods based on their argument types, the char version of operator >> is only assigning one character at a time, even though the whole string is buffered. I believe you're thinking that operator >> should try to put the whole string into your character; but since it "knows" you're reading one character at a time, it only assigns one character at a time. I'm not sure if this is specified behavior for cin or not, but that seems to be what's happening.

The cin operator reads from the standard input stream (if not configured otherwise). The stdin works as follows: you type and when you press Enter , it is sent to stdin and therefore cin reads the whole string up to the moment when you pressed Enter .

If you wish to read char by char, you should use getchar .

The way your keyboard input is seen by cin >> buffer; is not a property of your program, but of the combination of OS, Shell, C runtime and maybe thousand things I forgot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM