Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

c - Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted

I have faced this problem : Run-Time Check Failure #2 - Stack around the variable 'check' was corrupted in visual studio 12 . I also try this in codeblock but faced same problem . I run my code also in ideone.com it shows runtime error . IT works for Y but doesnot works for N

int main() {

int led=0;
    int ohm=0;
    char check;
    int flag=0;

while (led < 1 || led > 3){
    printf("Enter the number of switch you want to close: 

");
    printf("  ********************     Press 1 for switch (LED) 1     ********************
");
    printf("  ********************     Press 2 for switch (LED) 2     ********************
");
    printf("  ********************     Press 3 for switch (LED) 3     ********************
");

    printf("Switch: ");
    scanf("%d", &led);
}

printf("

");
while (ohm < 1 || ohm > 3){
    printf("Enter the resistance of Rheostat: 

");
    printf("  ********************     Press 1 for 10 ohm resistance  ********************
");
    printf("  ********************     Press 2 for 20 ohm resistance  ********************
");
    printf("  ********************     Press 3 for 30 ohm resistance  ********************
");

    printf("Resistance: ");
    scanf("%d", &ohm);
}


    while (flag == 0)
    {
        //LED-1
        if(led== 1 && ohm== 1 )
        {
            printf("LED-1 is blinking 2 times
");
        }

        if(led== 1  && ohm== 2)
        {
            printf("LED-1 is blinking 4 times
");
        }

        if(led== 1  && ohm== 3 )
        {
            printf("LED-1 is blinking 6 times
");
        }

        //LED-2
        if(led== 2  && ohm== 1 )
        {
            printf("LED-2 is blinking 2 times
");
        }

        if(led== 2  && ohm== 2 )
        {
            printf("LED-2 is blinking 4 times
");
        }

        if(led == 2  && ohm == 3)
        {
            printf("LED-2 is blinking 6 times
");
        }

        //LED-3
        if(led == 3  && ohm == 1 )
        {
            printf("LED-3 is blinking 2 times
");
        }

        if(led == 3  && ohm == 2)
        {
            printf("LED-3 is blinking 4 times
");
        }

        if(led == 3 && ohm == 3)
        {
            printf("LED-3 is blinking 6 times
");
        }

        printf("Do you want to continue Yes (Y) or No (N): ");
        scanf("%s", &check);

        if(check =='Y' || check =='y')
        {
            led = 0;
            ohm = 0;
            while (led < 1 || led > 3){
            printf("Enter the number of switch you want to close on: ");
            scanf("%d", &led);
            }

            while (ohm < 1 || ohm > 3){
            printf("Enter the resistance of Rheostat: ");
            scanf("%d", &ohm);
            }
        }

        if(check=='N' || check=='n')
        {
            printf("Thanks for using the program");
            flag = 1;
        }



    }
    return 0;

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the statement scanf ("%s", &check); you're trying to scan a string and stuff it into a char. There are a couple of ways to fix this issue:

Quick fix: replace scanf("%s", &check) with scanf (" %c", &check). Notice the whitespace in the format string: " %c", not just "%c". The %c format specifier does not skip leading whitespace, so you have to include whitespace before the %c in your format string to explicitly signal to scanf() that you want to skip the leading whitespace. If you don't, then the following will happen:

  1. the carriage return left in the input stream from the previous prompt Enter the resistance of Rheostat: will be taken as the input character. check will be assigned .

  2. Both if conditions will fail; check is neither Y nor y, neither N nor n.

  3. The execution will return to the top of the while loop for flag, which is still zero. So the output regarding the appropriate values of ohm and led will be displayed again, and then the prompt for check will be displayed again.

  4. scanf() will check the input stream again, this time reading the actual choice the user enters, and do the appropriate thing.

By including the leading whitespace in your format string, you will avoid this duplicate output.

A better fix: flush the input buffer after each call to scanf(). Define a macro:

#define FLUSH while (getchar() != '
')

and after each call to scanf(), type FLUSH;. It's a lot safer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...