简体   繁体   中英

Why is the while loop not working?

im new to c language and i am facing some problems with my code.. I have to make a program that computes the prices for renting cars but the while loop is not functioning properly.The scanf above the while condition is %s and not %c because it wont let me give a characther when it is %c.Thank you in advance.

int main(){
    float skms, ekms, tkms, price;
    char catcode;
    int days;

    do{
        printf("Selected rental program:\n");
        scanf("%c",&catcode);
        printf("Enter number of days:\n");
        scanf("%d",&days);
        printf("Starting kilometers display:\n");
        scanf("%f",&skms);
        printf("Kilometers display at end of rent:\n");
        scanf("%f",&ekms);
        tkms=(ekms/10)-(skms/10);
        switch(catcode){
                case'a':
                case'A':
                    price=ammountDueA(days, tkms);
                    printf("%.2f\n",price);
                    break;
                case'b':
                case'B':
                    price=ammountDueB(days, tkms);
                    printf("%.2f\n",price);
                    break;
                case'c':
                case'C':
                    price=ammountDueC(days, tkms);
                    printf("%.2f\n",price);
        }

        printf("Select rental program.('Q' or 'q' to exit)\n");



        scanf("%s",&catcode);


    }while(catcode==!'Q'&&catcode==!'q');
    printf("%c",catcode);


    return 0;
}

By "not working", I suppose you mean that it never enters a second iteration, but that is exactly what it is supposed to do. catcode==!'Q'&&catcode==!'q' is true if catcode is equal to !'Q' and equal to !'q' . 'Q' is some non-zero integer (depends on your system, but is probably 81), and !'Q' is zero. Similarly, !'q' is zero. catcode isn't zero, so the loop terminates.

There are two problems in your code:

First of all, the specifiers in scanf ...

scanf("%s", &catcode);

This won't work. The "%s" specifier expects its corresponding argument to be a char* to a buffer of unspecified length (ie: this has the same problem as gets() ). You probably meant "%c" , whose corresponding argument shall point to a single character . Now, your format specifier is not recommended to be "%c" , but " %c", because that extra space indicates scanf() to ignore all whitespace it can before "%c"`. So, that line becomes...

scanf(" %c", &catcode);

Now, you're right, there's a problem in your while loop:

while(catcode==!'Q'&&catcode==!'q');

Let's "expand" this with whitespace, to make what you (apparently) intended more obvious (that's reason #1 to use spaces between operators!)...

while(catcode ==! 'Q' && catcode ==! 'q');

For you it appears okay. Not for me. The C language does not define any operator ==! , rather, it defines operator == (that compares for equality), and operator ! (that negates its single operand). The compiler would have understood that line as...

while(catcode == !'Q' && catcode == !'q');

Now, what does that mean? If the operand is non-zero, the ! operator returns zero (in this case, the null character, '\\0' ). Otherwise, it returns a non-negative value (usually 1 (does the standard mandates this?)). Because 'Q' is not '\\0' , and same for 'q' , that means that your loop is effectively...

while(catcode == '\0' && catcode == '\0'); // Think of '\0' as 0

Absolute non-sense expressed in code! The solution is to simply use operator != (compares for inequality)...

while(catcode != 'Q' && catcode != 'q');

I hope this has led some light on you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM