Most inputted number wins

I want to make a program, which takes multiple inputs and counts the quantity of ones,twos,...
I have this 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
#include <iostream>

using namespace std;

int main()
{
   int cantidad;
   int num[9];
   int max=0;
   while(cin>>cantidad && cantidad){
        int number[cantidad];
       for(int n=0;n<cantidad;n++){
          cin>>number[n]; 
       }
       for(int u=0;u<9;u++){
           num[u]=0;
       }
       for(int x=0;x<cantidad;x++){
           switch(number[x]){
               case 1:
               num[0]+=1;
                case 2:
                num[1]+=1;
                case 3:
                num[2]+=1;
                case 4:
                num[3]+=1;
                case 5:
                num[4]+=1;
                case 6:
                num[5]+=1;
                case 7:
                num[6]+=1;
                case 8:
                num[7]+=1;
                case 9:
                num[8]+=1;
           }
       }
       for(int i=0;i<cantidad;i++){
           if(num[i]>max){
              max=i+1;
           }
       }
       cout<<max<<"\n";
       }
   
   
   return 0;
}

Why does it not work?
For one thing, it's unlikely you actually want all of those switch cases to fall through. Maybe some breaks?

Other than that, your description is too vague for me to understand what you want to do.
Variable names are important; try to make them meaningful. You have one array called "num" and then you have another array called "number". What is the difference in purpose between these two arrays? (After reading your post again, I realize what that difference is, but it's still a bit confusing when you first read it). I would change it to "digits" to actually store the digit counts.

Also, note that (once you add 'break;'s like dutch said), your switch statement can collapse down to simply:
num[number[x]-1] += 1; Now, I have no idea if that's supposed to be right or not, I'm just pointing out an observation.

Edit: Furthermore, you're re-creating your "number" array each loop iteration. Is that what you want? Furtherfurthermore, the way you're creating your number array is not legal C++ because arrays have to have compile-time constant sizes.

I suggest using a vector.
Edit: Just kidding, you don't even need a vector, because you don't need to actually store each iteration. You just need to update your histogram of digits.

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
// Example program
#include <iostream>
using std::cout;
using std::cin;

int main()
{
    int count;
    cout << "Enter number of items: ";
    cin >> count;
    
    cout << "Enter digits: ";
    int digits_histogram[10] {}; // init'd to all 0s
    
    for (int i = 0; i < count; i++)
    {
        int number;
        cin >> number;
        digits_histogram[number % 10]++;
    }
    
    for (int i = 0; i < 10; i++)
    {
        cout << "digit " << i << ": " << digits_histogram[i] << '\n';   
    }
}

Enter number of items: 7
Enter digits: 1 1 0 9 2 2 3
digit 0: 1
digit 1: 2
digit 2: 2
digit 3: 1
digit 4: 0
digit 5: 0
digit 6: 0
digit 7: 0
digit 8: 0
digit 9: 1

Last edited on
Topic archived. No new replies allowed.