So this code come directly from the book, like I literally copied it and it wouldn't run on my computer. I was going to use it on the exercises but it won't run. The program works by you inputting a large amount of numbers and then it says how many times the number you entered appears. Example, you input 46 46 46 46 55 55 100 100 100 and it outputs, "46 appears 4 times,55 appears 2 times, 100 appears 3 times" But the program itself never gives me the chance to input, it just runs and has me exit.
#include <iostream>
int main()
{
//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; //remembers the new value
cnt = 1; //resets the counter
}
} //While loop ends here
//remember the 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;
}