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.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 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)"<<endl;
	getline(cin,input);
	for(unsigned int i=0;i<input.length();i++)
	{
		if(input.at(i)=='0')
		{
			numof0++;
		}
		if(input.at(i)=='1')
		{
			numof1++;
		}
		if(input.at(i)=='2')
		{
			numof2++;
		}
		if(input.at(i)=='3')
		{
			numof3++;
		}
		if(input.at(i)=='4')
		{
			numof4++;
		}
		if(input.at(i)=='5')
		{
			numof5++;
		}
		if(input.at(i)=='6')
		{
			numof6++;
		}
		if(input.at(i)=='7')
		{
			numof7++;
		}
		if(input.at(i)=='8')
		{
			numof8++;
		}
		if(input.at(i)=='9')
		{
			numof9++;
		}
	}
	float total=numof0+numof1+numof2+numof3+numof4+numof5+numof6+numof7+numof8+numof9;
	if(numof0/total>0.5)
	{
		cout<<"The majority value is '0'" <<endl;
	}
	if(numof1/total>0.5)
	{
		cout<<"The majority value is '1'" <<endl;
	}
	if(numof2/total>0.5)
	{
		cout<<"The majority value is '2'" <<endl;
	}
	if(numof3/total>0.5)
	{
		cout<<"The majority value is '3'"<<endl;
	}
	if(numof4/total>0.5)
	{
		cout<<"The majority value is '4'"<<endl;
	}
	if(numof5/total>0.5)
	{
		cout<<"The majority value is '5'"<<endl;
	}
	if(numof6/total>0.5)
	{
		cout<<"The majority value is '6'"<<endl;
	}
	if(numof7/total>0.5)
	{
		cout<<"The majority value is '7'"<<endl;
	}
	if(numof8/total>0.5)
	{
		cout<<"The majority value is '8'"<<endl;
	}
	if(numof9/total>0.5)
	{
		cout<<"The majority value is '9'"<<endl;
	}
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.