ccharscanf

why is this program skipping the the scanf in this program


anytime i run this function in my program

void treeInput(){
    char temp;
    int x, tempRow, tempColm;
    while(x=0){
        scanf(" %c ", &temp);
        if(temp == 'Q'){
            x=1;
        } else if(temp == 'A'){
            for(int i = 0; i<TEMP; i++){
                for(int j = 0; j<TEMP; j++){
                    forest[i][j] = 'T';
                }
            }
        } else if (temp == 'T'||'E'){
            scanf("%d", &tempRow);
            scanf("%d", &tempColm);
            forest[tempRow][tempColm] = temp;
        }
    }
}

it skips the scanf making the rest of the function null and void and idk how to make it work

i tried to add a space before the %c in the scanf but that didn't work, it still skips it


Solution

  • You used an assignment (=) where you meant to use a comparison (==).

    while(x=0) ...
    

    should be

    while(x==0) ...
    

    But now we're checking x before giving it a value.

    int x;
    

    should be

    int x = 0;
    

    temp == 'T'||'E'
    

    is equivalent to

    temp == ('T'||'E')
    

    When the left-hand size of || is a non-zero number as is the case here, it evaluates to 1. So the above is equivalent to

    temp == 1
    

    You want

    temp == 'T' || temp == 'E'