Wont print 100

Write your queThe book says to print 100 3 times by inputting 42 42 42 42 42 55 55 62 100 100 100 but it doesnt print "100 occurs 3 times"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
	}
Is it possible this is the same problem you had last time?
Remember that you have to do something to tell the program that there are no more numbers to input.
You can either enter something non-numeric (like abc) or you can signal the end-of-file condition (Ctrl-Z on windows, Ctrl-D on linux).
When I run it through the c++ shell this is the output

1
2
3
4
42 occurs 5 times
 55 occurs 2 times
 62 occurs 1 times
 100 occurs 3 times
The 100 part is missing when I go to press ctrl + F5. This is a program in a book called C++ Primer by Stanly Lippman. Another question: if the above program is hard for me to understand--in its entirety, do I have any business learning how to program?
1
2
3
4



42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times










How do I make it run like this
Why would you want it to run like that?
You're not making any sense!
If you enter 1 2 3 4 it should print

1 occurs 1 times
2 occurs 1 times
3 occurs 1 times
4 occurs 1 times

Anyway, about the missing "100 occurs 3 times" line.
Did you do what I told you to do (and what you were told in your last thread)?
The program isn't going to know you're done entering numbers until you tell it, either by signalling end-of-file or entering something non-numeric.
I meant the second part:
42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times

I want it like that because thats what the book says should come out! I already tried what you said and it worked but I want what the book says should come out, Im using visual studio and ctrl + F5'ing. Would you say then that the book is wrong? Also could you answer my non-technical question: " if the above program is hard for me to understand--in its entirety, do I have any business learning how to program? "
Anybody?
I'm confused. Do you want it to print "X occurs Y times" interlaced with further user input, or do you only want it to print at the end, after all user input?

If it's the latter, this can definitely be hard for a beginner if you don't understand the concept of a map, because you need to save the results somehow, and so you either need an array of {number, num. occurrences} pairs, or a more efficient container like a tree.
Hello Pepeforever,

STOP pressing:
Ctrl + F5 (Start without debugging) and just use F5 (Start debugging).

The debugging information added to the ".exe" file will be of more use to you.

After that the while loop will need some rethinking or enter the numbers into a vector or array and work with that.

Andy
If the above program is hard for me to understand--in its entirety, do I have any business learning how to program?

A beginner can't be expected to understand everything about this program.

Programming is intrinsically challenging. If you enjoy this, find it rewarding, relaxing, entertaining, engaging, or want to learn for any reason, yes, you have business learning to program.
Your program works fine. The problem is something to do with the way you're running it.
@ Ganado

I want it to print

42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times
> The 100 part is missing when I go to press ctrl + F5

The last number won't be printed inside the loop. Print it at the end, after the loop exits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <sstream>

int main()
{
    //  by inputting 42 42 42 42 42 55 55 62 100 100 100
    // simulate the input (in the actual program, type in these values)
    std::stringbuf strbuf( "42 42 42 42 42 55 55 62 100 100 100" ) ;
    std::cin.rdbuf( std::addressof(strbuf) ) ;

    int prev ;
    if( std::cin >> prev )
    {
        int cnt = 1 ;
        int curr ;
        while( std::cin >> curr )
        {
            if( curr == prev ) ++cnt ;

            else // curr != prev
            {
                std::cout << prev << " occurs " << cnt << " times\n" ;
                cnt = 1 ;
                prev = curr ;
            }
        }

        // at the end, print the last item (which would not have been printed inside the while loop)
        std::cout << curr << " occurs " << cnt << " times\n" ;
    }
}

http://coliru.stacked-crooked.com/a/7b542a9a129ca0c8
Topic archived. No new replies allowed.