C++ Primer 5th ed question

it says: We can use an if to write a program to count how many consecutive
times each distinct value appears in the input:
the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// currVal is the number we're counting; we'll read new values into val
    int currVal = 0, val = 0;
    
    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
            }
        }

        std::cout << currVal << " occurs "<< cnt << " times" << std::endl;
    }
    return 0;



-thank you jlb for correcting me.

for some questions now:
the book didn't really explain how the input works in this situation -
does the if grab the first number in the input chain say 12 12 12 13 it would grab 12
then does the while step through grabbing the next available number in the stream and compare it to the 12 (currVal) ?
Just can't find an explanation of what the cin in the while loop is doing - does it grab the first value - go into the body of the loop - then increment to the the next number (seperated by a space) - until an EOF? I'm also confused on how that second cin (in the while loop) has access to the same input stream - my understanding is if you input:
12 12 13 14 15
on the first go around currVal will hold the first 12 and the val will hold the second - they are equal so cnt is 2 - then val=13 and its not equal to 12 so it displays the count of 12 and sets currVal to 13 and so on

thank you
Last edited on
it should say 12 occurs 4 times no excuse.

Why 4? I count 3 consecutive occurrences of 12.
it says: We can use an if to write a program to count how many consecutive

The last 12 is not consecutive.


By the way when posting code please use code tags (the "<>" icon to the right) or [code] Code here [/code]

Thanks jlb - fixed now with some questions
Last edited on
Please don't alter your posts after the fact, just add a new post to the topic with the new questions.

Just can't find an explanation of what the cin in the while loop is doing

It extracts a value from the console and places it into the variable "value" unless the input stream fails. If the stream doesn't fail it proceeds into the loop body. If the stream fails the loop terminates. There are several ways a stream can fail, it can encounter eof, it could encounter an invalid entry, like a character when it is expecting a numeric value, and several others.

I'm also confused on how that second cin (in the while loop) has access to the same input stream

You can access a stream many many times, when dealing with the console, the input operation will wait for the user to enter another value before doing anything else.

the book didn't really explain how the input works in this situation -
does the if grab the first number in the input chain

The first input operation, the one in the if statement grabs the first value placing it in the variable "currVal", the input operation in the while places the next input into the variable "val".


Topic archived. No new replies allowed.