I am trying to make a program for counter how many times i enter a number separated by a comma. Entering 5 at a time. Im so new to c++ . i have been learning Visual Basic and learned i was going no were with it. so decided to take up C++.
Use a std::map or a std::unordered_map - with the key being the entered integer and the associated mapped data being the number of times it was entered. Each time you read in a number, increment the mapped data.
See: http://www.cplusplus.com/reference/stl/map/
Something like:
1 2 3 4
std::map<int,int> frequency_map ;
int number ;
// read in number
++ frequency_map[number] ;
Finally, iterate through the frequency_map and print out the results.
If the numbers being entered are expected to be in a small range like 1 to 10 (you would need to validate the input for range), a simple array with the position as the number and the value as its frequency could be used.
#include <iostream>
#include <stdio.h>
int main(int argc, char *argv[]) {
int arr[5] = {0};
int i=0;
int x=0;
printf("Enter ten integers:\n");
for (i=0; i<5; i++) {
scanf("%d", &x);
if(x >= 0 && x <= 57) arr[x]++;
}
//below is ok and right
for (i=0; i<57; i++) {
printf("%d seen %d times\n", i, arr[i]);
}
system("pause");
return 0;
}
but couldnt figure out how to make it so that i could enter any amount then press x and enter to start the count