### Majority number in a string problem

Hello guys, I'm trying to make a program where the user inputs a sequence of numbers I.E 1 0 0 3 4 0 6 0 0 (numbers separated by spaces) and the program has to tell them the majority number, in this case '0'. I have compiled a program, however it only accounts for single digits. In example, if the user inputs, 800, 500, 800, 800, the program will tell them that the majority number is '0', however in reality, it is 800 (since it is separated by spaces from the other numbers). I have no idea on how to approach fixing it. Here's my code.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` `````` string input; int numof0=0; int numof1=0; int numof2=0; int numof3=0; int numof4=0; int numof5=0; int numof6=0; int numof7=0; int numof8=0; int numof9=0; cout<<"Enter sequence of numbers (separated by spaces)"<0.5) { cout<<"The majority value is '0'" <0.5) { cout<<"The majority value is '1'" <0.5) { cout<<"The majority value is '2'" <0.5) { cout<<"The majority value is '3'"<0.5) { cout<<"The majority value is '4'"<0.5) { cout<<"The majority value is '5'"<0.5) { cout<<"The majority value is '6'"<0.5) { cout<<"The majority value is '7'"<0.5) { cout<<"The majority value is '8'"<0.5) { cout<<"The majority value is '9'"<
What you do is what you get. What is the problem? Do you not understand what you are writing in your program?
I know what the problem is. I know what I am writing. I simply just need ideas on how to approach this problem.
Since you simply just need ideas... My idea would be to use a map from the standard template library.
http://www.cplusplus.com/reference/map/map/
Last edited on
instead of using `getline` use `cin >> `.
You can store input in an integer instead of a string (the standard library will do the conversion for you.)

That said, I would use `std::vector<int>` to store all of the inputs, then use `std::count //http://www.cplusplus.com/reference/algorithm/count/ ` to determine the quantity of a specific number in the vector.

kevinjt2000 wrote:
My idea would be to use a map
Why would you use a map for this? o_o
Last edited on
A map could just have each member ++'ed and then iterate at the end over members of the map that have been initialized. It keeps count.
Oh, okay. Fair. Hadn't thought of that. But now I feel like I should have. :P
I withdraw my argument.
Last edited on
Topic archived. No new replies allowed.