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 :)