Program stops outputting data?? (Beginner)

Hi, I am going through a book about C++ at the moment. I understand the concept in this question about if else. One of the things it wants me to do is first put the code in my computer exactly and make sure it runs right before moving on. I've looked over it 3+ times and don't see anything different from the book.

It is supposed to take an input of numbers "33 33 42 42 45" then shoot out "X occurs Y times". It does this for 3 of them max, but any more different numbers then three it just acts like it isn't there.

I'm using Microsoft Visual Studio if that makes a difference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
int main()
{
	int currVal = 0, val = 0;
	//read first # to make sure there is input
	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;
			}
		}
	}
	return 0;
}


Here's what happens when I put in some random input,
http://i.imgur.com/6syi991.png

thanks
The only time lines 12 and 13 are executed is when you enter a value that is different from the last value entered. You don't do that for the the last set of values.
After reading your reply I went back and noticed I missed a line at the bottom. After adding it, it still does the same thing. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
int main()
{
	int currVal = 0, val = 0;
	//read first # to make sure there is input
	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;
}
From the picture of your input you don't do anything that would cause std::cin >> val to not succeed, so the while loop is never terminated, and lines 18 and 19 are never reached. You might try entering a letter to terminate the input.
Yeah, like cire said, you could say while (val!=0) and then 0 would terminate the program.
Topic archived. No new replies allowed.