-1
#include <iostream>

int main()
{
    int currVal = 0, val = 0;

    if (std::cin >> currVal) {
        int cnt = 1; 

        while (std::cin >> val) { 
            if (val == currVal)
                ++cnt; 
            else 
            {
                std::cout << currVal << " occurs " << cnt << " times" << std::endl;
                currVal = val; 
                cnt = 1; 
            }
        } 

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

    return 0;
}

If we give this program the following input:

42 42 42 42 42 55 55 62 100 100 100

then the output should be

42 occurs 5 times

55 occurs 2 times

62 occurs 1 times

100 occurs 3 times

All this code should work properly. Sorry for the inconvenience.(had a brain fart)

  • 1
    `cin >> x` used as a condition will be false if the read fails, which is really important in making sure you only act on good input. – chris Mar 03 '13 at 17:19

2 Answers2

3

This appears to work as expected.

There is a very good reason to do std::cin >> whatever in the condition of an if statement or while loop. The extraction operator >> returns the stream that you're operating on. So the value of the std::cin >> whatever expression is std::cin itself.

A stream, such as std::cin, can be converted to bool for the sake of checking whether the stream is in a valid state. Consider:

if (std::cin >> whatever)

This can be understood as taking input from the user into the variable whatever and then checking if the input was valid or not. If not, the if block will not execute.

Similarly:

while (std::cin >> whatever)

This will loop, taking input from the user each time, until the user enters something invalid.

What is an invalid input? Well if whatever in the above examples was an int, and the user entered the text blah, this would fail because it's not a valid integer.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

The code you have given works as intended.

The expression stream >> variable produces a reference to the stream, and as an if condition this is then converted to bool as if you had written !(stream >> variable).fail().

However, while the code works technically, it has much redundancy

And so, except if the author’s point was to illustrate technically ungood code (perhaps with a discussion of all that’s wrong with it) it’s ungood pedagogically.

Here is an alternative formulation with much less redundancy:

#include <iostream>
using namespace std;

int main()
{
    int current_value = 0;
    int run_length = 0;

    for( bool at_end = false; !at_end; )
    {
        int value;
        at_end = (cin >> value).fail();

        if( run_length > 0 && (at_end || value != current_value) )
        {
            cout << current_value << " occurs " << run_length << " times" << endl;
            run_length = 0;
        }

        if( !at_end )
        {
            if( run_length == 0 )
            {
                current_value = value;
            }
            ++run_length;
        }
    }
}

Testing:

[D:\dev\test]
> g++ foo.cpp

[D:\dev\test]
> echo 42 42 42 42 42 55 55 62 100 100 100 | a
42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times

[D:\dev\test]
> _
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331