Need help! Count how many times # entered

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++.

PLEASE HELP ME
Help us help you. What have you done so far. What is the issue you are having.
well im currently re reading the documentation . . . . .

i was writing the wrong code to begin with so i have to start from scratch. . . .


i was wring VB code and it wasnt even close . . . .


i got this from searching on the web
#include <stdio.h>

int main(int argc, char *argv[]) {
int arr[10] = {0};
int i=0;
int x=0;

printf("Enter ten integers:\n");

for (i=0; i<10; i++) {
x = scanf("%d", &i);
}

for (i=0; i<10; i++) {
printf("%d seen %d times\n", i, arr[x]);
}

return 0;
}




it says its outdated.

using microsoft c++




Read a complete line with std::getline()
http://www.cplusplus.com/reference/string/getline/

Use a std::istringstream to extract data from the line.
http://www.artima.com/cppsource/streamstrings3.html

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <sstream>

int main()
{
    std::string line ;
    std::getline( std::cin, line ) ; // 

    std::istringstream stm(line) ;
    const char COMMA = ',' ;
    int number ;
    char delimiter ;
    int count = 0 ;
    while( stm >> number )
    {
        ++count ;
        if( stm >> delimiter && delimiter==COMMA ) continue ;
        else break ;
    }
    
    std::cout << count << '\n' ;
}


Repeat for each line entered.
Last edited on
mhmmmm
can u recommend me a c++ compiler?

keeps saying " This project is outdated"
The current version of the Microsoft C++ compiler would be adequate.

Or you could use (compiler+IDE, Windows) one of these:

Code Blocks http://sourceforge.net/projects/codeblocks.berlios/files/codeblocks-10.05mingw-setup.exe/download

CodeLite http://sourceforge.net/projects/codelite/files/Releases/codelite-3.5/codelite-3.5.5377-mingw4.6.1.exe/download
Last edited on
JLBorges thats nice for find how many you entered but i wanna like show what you entered and how many times u entered it.


like


Enter a Number(s): 2,2,2,5,4

1 used: 0 times
2 used: 3 times
3 used: 0 times
4 used: 1 times
5 used: 1 times
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.
i had this
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
#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
Topic archived. No new replies allowed.