简体   繁体   中英

C++ primer 5th 1.4.4

I'm a beginner of C++,while reading the book 《C++ Primer》 5th,I'm a little confused in chapter 1.4.4. when I run the program in 1.4.4,here is the step in my computer:

#include <iostream>
int main()
{
    // currVal is the number we're counting; we'll read new values into val
    int currVal = 0, val = 0;

    // read first number and ensure that we have data to process
    if (std::cin >> currVal) 
    {
        int cnt = 1;  // store the count for the current value we're processing

        while (std::cin >> val) 
        { // read the remaining numbers

            if (val == currVal)   // if the values are the same
                ++cnt;            // add 1 to cnt
            else 
            { // otherwise, print the count for the previous value
                std::cout << currVal << " occurs " << cnt << " times" << std::endl;
                currVal = val;    // remember the new value
                cnt = 1;          // reset the counter
            }

        }  // while loop ends here

        // remember to print the count for the last value in the file
        std::cout << currVal <<  " occurs " << cnt << " times" << std::endl;
    } // outermost if statement ends here

    return 0;
}
  1. type the numbers:42 42 42 42 42 55 55 62 100 100 100
  2. type Ctrl+D
  3. the program run by itself(not waitting me to input Enter)
  4. output the answer:

42 occurs 5 times
55 occurs 2 times
62 occurs 1 times

  1. secondly type Ctrl+D
  2. output the remained answer

    100 occurs 3 times

my question is why I have to input second times of Ctrl+D,my code environment is Ubuntu+GCC,I also run it in VS2013,it only needs to input once Ctrl+D.

I've searched in stackoverflow,but I didn't got my answer.

Incorrect output. C++ primer 1.4.4

confused by control flow execution in C++ Primer example

C++ Primer fifth edtion book (if statement) is this not correct?

In Linux Ctrl+D does not unconditionally mean "end-of-file" (EOF). What it actually means is "push the currently pending input to whoever is waiting to read it". If the input buffer in non-empty, then hitting Ctrl+D does not create EOF condition at the end of the buffer. Only if you hit Ctrl+D when the input buffer is empty , only then it will produce EOF condition. (See here for a more technical explanation: https://stackoverflow.com/a/1516177/187690 )

In your case you are inputting your data as a single line and then hitting Ctrl+D at the end. This pushes your input to your program and makes your program to read and process the data. But it does not produce EOF condition at the end of your input.

For this reason, once all input data is read by the cycle, your program does not see it as EOF. The cycle keeps waiting on empty input buffer for additional data. If at this point you press Ctrl+D again, it will be recognized as EOF and your program will exit the cycle and print the last line.

This is why you have to hit Ctrl+D twice: the first hit works pretty much as Enter key would. And only the second hit creates EOF condition.

It is possible that the program you have provided was not intended to accept input all at once as you have shown.

The reason it does not provide the output you expect is due to the fact that the program is still expecting input, because the >> operation's return value is still logically true/error-free. (It's blocked at: while (std::cin >> val) ) This is so, because you have not provided an EOF to the input stream after the last 100. Said another way, your first Ctrl+D gets past the if (std::cin >> currVal) . Your second Ctrl+D gets past the while (std::cin >> val) .

See the accepted answer to this question for why the first Ctrl+D does not result in a eofbit error on your input stream: Why do I have to type ctrl-d twice? The bottom line is that Ctrl+D does not necessarily mean EOF; it results in a flush of the input stream.


Entering the numbers one at a time would provide the output you expect.

Alternatively, you could provide: 42 42 42 42 42 55 55 62 100 100 100\\n.

http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

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