简体   繁体   中英

Why doesn't this program stop?

My instructor has asked us to follow the variables through this code and determine when the variables change. He says the inputs should be

8, 4, 2, 1

I have compiled and run the code to he me understand it but it doesn't stop. It just ouputs "Feed me two numbers please:" over and over. Any help is greatly appreciated.

#include <stdio.h>
main ()
{
    int a;
    int b;
    int c=0;
    int d=0;
    int e=0;
    int f=0;

    while (c == 0 || a + b !=0){
        printf("Feed me two numbers please: \n");
        scanf ("%d %d", &a, &b);
        if (c == c + 1){
            printf("Welcome to my world!\n\n");
        }
        if (c = 0){
            d = a + b;
            e = d;
        }
        else if (a + b > d){
            d = a + b;
        }
        else if (a + b < e){
            e = a + b;
        }
        if (a < f){
            f=a;
        }
        c = c + 1;
    }

    printf("Now hear this:%d %d\n\n", d, e, f);
}

In

if (c = 0)

you're assigning 0 to c , the expression of the assignment returns the assigned value , so the expression will be always evaluated to false as it's equivalent to if(0) , it should be if(c == 0) .

Also

if (c == c + 1)

doesn't make any sense, what exactly do you mean? I think it should be c > 0 .

In all cases, you should use the debugger, it can save you a lot of time, and will help you to really understand your code.

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