fork(2) download
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. // currVal is the number we're counting; we'll read new values into val
  6. int currVal = 0, val = 0;
  7.  
  8. // read first number and ensure that we have data to process
  9. if (std::cin >> currVal) {
  10. int cnt = 1; // store the count for the current value we're processing
  11. while (std::cin >> val) { // read the remaining numbers
  12. if (val == currVal) // if the values are the same
  13. ++cnt; // add 1 to cnt
  14. else { // otherwise, print the count for the previous value
  15. std::cout << currVal << " occurs "
  16. << cnt << " times" << std::endl;
  17. currVal = val; // remember the new value
  18. cnt = 1; // reset the counter
  19. }
  20. } // while loop ends here
  21. // remember to print the count for the last value in the file
  22. std::cout << currVal << " occurs "
  23. << cnt << " times" << std::endl;
  24. } // outermost if statement ends here
  25. return 0;
  26. }
Success #stdin #stdout 0s 2732KB
stdin
42 42 42 42 42 55 55 62 100 100 100
stdout
42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times