user2463808
user2463808

Reputation: 179

Can't compare characters

I have 2 problems:

first problem is that I cant in for (apr = 0; apr < aprno; apr++) add chars one by one. For example, if I have aprno = 4, after entered A it asks for 4th apr., but It works when I enter AAAA..., also it works with integers only

second problem is char and int comparsion. I know that I cant compare them, but I didnt have found solution how to do it anywhere.

addnoaprons:
    system("cls");
    printf("Add number of available aprons: ");
    scanf("%d", &aprno);
    goto addtypeaprons;

addtypeaprons:
    if (aprno < 1) goto addnoaprons;
    else {
        system("cls");  
        printf("Add types for %d aprons total:", aprno);
        for (apr = 0; apr < aprno; apr++)
            {   
                system ("cls");
                printf("Aprons total: %d", aprno);
                printf("\n\nNo. %d apron type: ", apr + 1);
                scanf("%c", &pismapr[apr]);
                if (pismapr == 'A') poleapr[apr] = 1;
                if (pismapr == 'B') poleapr[apr] = 2;
                if (pismapr == 'C') poleapr[apr] = 3;
                if (pismapr == 'D') poleapr[apr] = 4;
                else goto addtypeaprons;
            }
            goto showaprons;
        }

Upvotes: 2

Views: 229

Answers (2)

haylem
haylem

Reputation: 22663

Problem 1: Huh?

I have no clue what your first problem is and couldn't really understand what you were asking for that one. Sorry.

Problem 2: Comparing ints to chars

Yes, you can compare ints and chars, within reason. What you probably don't want to do it to compare the address of a pointer to a char though.

When I see your question and this:

scanf("%c", &pismapr[apr]);

I can only assume that you mean that pismapr is an array of chars or ints, as I don't see the declaration. So you probably want to switch these:

if (pismapr == 'A')

to:

if (pismapr[apr] == 'A')

But that's assuming the type of pismapr, which we don't know with your snippet.

Problem 3: Gotos

Lose them. Really. Or tell me why you need them.

Problem 4: Invoking system()

Just don't. It's a bad idea, and you probably can do that some other way. If you really want to do this, are you sure you need to do all that with a C program, and not a shell script instead?

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

pismapr looks to be an array of char; you can't compare it directly to a single char. You need to compare just the array element of interest:

if (pismapr[apr] == 'A') poleapr[apr] = 1;

P.S. I have to tell you, this is some of the weirdest looking C code I've seen in ages. Instead of jumping around with goto, you should move blocks of code into functions, and then call them in a loop; i.e.,

while (aprno < 1)
    aprno = readaprno();

Upvotes: 5

Related Questions