i'm trying to count how many times a user inputs a certain digit and assign total number of instances to a location in a 10 row array (0-9). For instance if the user inputs 888
, it will assign 3
to location arr1[8].
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr1[10] = {0};
int c;
while ((c = getchar()) != '\n'){
for (int i = 0; i <= 9; i++){
if (c == i) // This isn't doing what I want it to do
arr1[i] += 1;
}
}
for (int i = 0; i <= 9; i++)
printf ("%c ", arr1[i]);
}
The trouble seems to be the line that i've added to comment above. The line if (c == i)
is intended to compare a user inputed digit (as it's entered by the user, not the ASCII value) and compare it with i
.
So how can I compare c
and i
as the same type? I've tried changing the type of getchar()
to char, signed int, unsigned int
but it doesn't seem to make any difference.