Reputation: 301
i need some help, can't seem to locate the problem, my program is supposed to take only octal digits and then count number of 4's and print that but the 4's counter doesn't work.
#include <stdio.h>
#include <string.h>
int main (void) {
char okt[6];
int i, broj, brojac = 0;
gets(okt);
broj = strlen(okt);
for (i = 0; i < broj; i++) {
if (okt[i]>'7' || okt[i]<'0')
printf("Ucitani niz nije pravilno zadan ");
else
if (okt[i] == 4)
brojac++;
}
printf("Znamenka 4 se pojavljuje %d puta %d", brojac);
return 0;
}
Upvotes: 2
Views: 188
Reputation: 4738
As you are comparing char
, put single quote around 4
.
else if (okt[i] == '4') brojac++;
Also there are two %d
s in the below statement expecting two int values. So your second %d
will print 0
.
printf("Znamenka 4 se pojavljuje %d puta %d", brojac); <-- Expecting two integers
Upvotes: 1