Why does this program work?

I recently wrote a program that enters char data, checks if this is valid (az, # indicates the end of the input), and pushes it on the stack, which then checks to see if it is a palindrome. I expected to enter it in one char, but if I enter a line ending with a pound, it works. Here are 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 line, for example "racecar #" and presses enter, the program correctly puts it on the stack. My question is simple: why? Shouldn't the data have to enter one char at a time in order for it to work correctly, because cin is in the loop itself, and the loop has to be repeated to push several characters onto the stack, right? Thanks!

Edit: Thanks for the answers / comments to everyone! I am very impressed with the quick and kind answers. I am sure I will use this site again.

+4
source share
5 answers

Logging into the console (via the cin std::istream ) is buffered line by line on most systems. Therefore, when you call cin::operator>> for a single character, the function does not actually return until you hit a new line (because the underlying I / O system does not make data available for cin until then). Any data entered before and after <newline> will be buffered, and subsequent calls to cin::operator>> will be served from the buffer until it is exhausted.

In this case, cin >> buffer , where buffer is of type char , will actually receive one character, but before that, the console will buffer the entire string and use it for subsequent input operations to the console.

If you execute your code in your debugger, the operation may be more understandable to you.

+4
source

The "system" (OS, library, regardless of implementation) depends on the line of data coming from the input, but your program reads its char on char.

+3
source

Although all answers about os buffering are correct, I think confusion can be traced back to cin operator >> (char) ; because C ++ can overload methods based on its argument types, the char operator >> version assigns only one character at a time, although the entire line is buffered. I suppose you think operator >> should try to put the whole line in your character; but since he โ€œknows,โ€ you read one character at a time, he assigns only one character at a time. I am not sure if this is indicated behavior for cin or not, but this is similar to what is happening.

+2
source

The cin statement reads from the standard input stream (unless configured otherwise). stdin works like this: you type, and when you press Enter , it goes to stdin , and so cin reads the entire line until you press Enter .

If you want to read char on char, you must use getchar .

0
source

The way to enter your keyboard using cin >> buffer; It is not a property of your program, but a combination of the OS, Shell, C, and possibly thousands of things that I forgot.

0
source

Source: https://habr.com/ru/post/1393221/


All Articles