user2627736
user2627736

Reputation: 301

C - Counter not working

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

Answers (2)

Digital_Reality
Digital_Reality

Reputation: 4738

As you are comparing char, put single quote around 4.

else if (okt[i] == '4') brojac++;

Also there are two %ds 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

Daniel
Daniel

Reputation: 2185

Should this okt[i] == 4 be okt[i] == '4'?

Upvotes: 3

Related Questions