problem with -->while (std::cin >> currNum)

Hi, im trying to learn C++ again and im a bit confused here.
Im trying to build a program that would count how many equal numbers are in users input like for example (22 45 65 65 22)
And i want to get results like

22 = 2 times
45 = 1 times
65 = 2 times

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
	
	int number = 0;
	int i = 1;
	while(std::cin >> number){
		std::cout<< i <<". number is " << number <<std::endl;
		++i;
	}
	return 0;
}


So 1st i succesfully at least was able to display inputed numbers.
My question is how could i now get 1st number from my list within this while loop and compare it to all the other numbers in the list to count how many times its equal?
I tryed to add another while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main(){
	
	int currNum = 0, number = 0;
	while(std::cin >> currNum){
		int cnt = 1;
		while(std::cin >> number){ //here i try to get this list of inputs again
			if(currNum == number)
				++cnt;
			else{
				std::cout<<currNum<<" = "<<cnt<<" times"<<std::endl;
				cnt = 1;
			}
		}
	}
	return 0;
}


Im getting output like this if i enter 32 34 4
32 = 1 times
32 = 1 times

if i run it again with the same 3 numbers im getting this
32 = 1 times
32 = 1 times
32 = 1 times

Now im getting 3 lines of output instead of 2, but its actually the same input.
So how should i try to build this. Hard for me to build this coz i dont understand how and where those X amount of imputs are stored. Any help how to build this would be appreciated :)
My question is how could i now get 1st number from my list within this while loop and compare it to all the other numbers in the list

You can't because you don't actually have a list. In other words you have no history of what the user has entered, as soon as one iteration finishes, your program then 'forgets' the current number.
i'd use a std::vector and in each iteration, push_back your values, then outside of your while you would have a list off all entered numbers to apply your counting logic.

edit: actually i'd use a map<int,int> i.e. it stores each number and how many times it's been typed.
Last edited on
You store input in 'number' The problem is that every new read overwrites the previous value and thus you only have one number stored, the latest input.

You should use a container. There is one that fits this case like a glove: the std::map
http://www.cplusplus.com/reference/map/map/

When you have a new number, there are two possibilities:

1. The map does not yet have an entry with the number as key.
Add new entry to the map with number as key and 1 as value.

2. The map has such entry. Increment the value of that entry.


When all input has been processed, the map will have all unique values and their counts. Now you can print the results.
Ty guys very much, i will definetly check this out :)
shout if you have a problem. i coded up an example while i was eating me lunch.
Topic archived. No new replies allowed.