Invalid types 'int[int]' for array subscript

closed account (21vXjE8b)
Hey guys! I'm with a little problem... This code works like this (it's in portuguese and I couldn't translate it --> I'm in a hurry!): you put a number and choose a digit. It'll return the number of times the digit appears in the number. In line 29 I'm getting the error of the title... What's wrong?

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
#include <iostream>
#include <locale>
using namespace std;

int contadigitos(int, int);

int main()
{
	
	setlocale(LC_ALL, "Portuguese");
	
	int num;
	int digito;
	
	cout << "Insira um número: "; 
	cin >> num;
	cout << "Insira um dígito: ";
	cin >> digito;
	
	contadigitos(num, digito);	
}

int contadigitos(int n, int d)
{
	int contador = 0;
		
	for (int i = 0; i < n; i++)
	{
		if (n[i] == d) contador++;
	}
	
	cout << "O dígito " << d << " " << "aparece em " << n << " " << contador << " " << "vezes.";
}
Olá

neither num nor n have been declared as array....

hope it helps
closed account (21vXjE8b)
Now I realized that in line 27 I was comparing i with n, not the length of n O.o
Maybe this wasn't the problem, but I considered num and n as strings, did some slight changes and the code worked.
Obrigado mesmo assim (Thank you anyway) ^-^
Last edited on
bem vinda
Topic archived. No new replies allowed.