简体   繁体   English

运行时检查失败#2-变量'check'周围的堆栈已损坏

[英]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 . 我遇到了这个问题:运行时检查失败#2-变量'check'周围的堆栈在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 . 我也在ideone.com中运行我的代码,它显示运行时错误。 IT works for Y but doesnot works for N IT适用于Y但不适用于N

int main() { 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: \n\n");
    printf("  ********************     Press 1 for switch (LED) 1     ********************\n");
    printf("  ********************     Press 2 for switch (LED) 2     ********************\n");
    printf("  ********************     Press 3 for switch (LED) 3     ********************\n");

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

printf("\n\n");
while (ohm < 1 || ohm > 3){
    printf("Enter the resistance of Rheostat: \n\n");
    printf("  ********************     Press 1 for 10 ohm resistance  ********************\n");
    printf("  ********************     Press 2 for 20 ohm resistance  ********************\n");
    printf("  ********************     Press 3 for 30 ohm resistance  ********************\n");

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


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

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

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

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

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

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

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

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

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

        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;

} }

scanf("%s", &check); should be scanf("%c", &check); 应该是scanf("%c", &check); as you reading a char not a string. 当您阅读char而不是字符串时。

The problem is that your variable "check" is too small. 问题是您的变量“ check”太小。

scanf("%1s", check);

Because you are saving a string (with terminating \\0) you should use a larger variable: 因为要保存一个字符串(以\\ 0结尾),所以应使用一个较大的变量:

char check[2];

Edit: But yes, scanf("%c", &check) is far better if input is only one single character. 编辑:但是,如果输入仅是一个字符,scanf(“%c”,&check)会更好。

In the statement scanf ("%s", &check); 在语句scanf ("%s", &check); you're trying to scan a string and stuff it into a char. 您正在尝试扫描字符串并将其填充为char。 There are a couple of ways to fix this issue: 有两种方法可以解决此问题:

Quick fix : replace scanf("%s", &check) with scanf (" %c", &check) . 快速修复 :将scanf("%s", &check)替换为scanf (" %c", &check) Notice the whitespace in the format string: " %c" , not just "%c" . 注意格式字符串中的空格: " %c" ,而不仅仅是"%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. %c格式说明符不会跳过前导空格,因此您必须在格式字符串中的%c之前包括空格,以便向scanf()明确表示要跳过前导空格。 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. 从上一个提示输入流中返回的回车符。输入Enter the resistance of Rheostat:将作为输入字符。 check will be assigned \\n . check将被分配\\n

  2. Both if conditions will fail; 这两种if条件将失败; check is neither Y nor y , neither N nor n . check既不是Y也不是y ,也不是N也不是n

  3. The execution will return to the top of the while loop for flag , which is still zero. 执行将返回到flagwhile循环的顶部,该flag仍为零。 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. 因此,将再次显示有关ohmled的适当值的输出,然后将再次显示check提示。

  4. scanf() will check the input stream again, this time reading the actual choice the user enters, and do the appropriate thing. scanf()将再次检查输入流,这一次将读取用户输入的实际选择,并执行适当的操作。

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() . 更好的解决方案 :在每次调用scanf()之后刷新输入缓冲区。 Define a macro: 定义一个宏:

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

and after each call to scanf() , type FLUSH; 在每次调用scanf() ,键入FLUSH; . It's a lot safer. 更安全。

Its working fine for me in code-blocks .... flush all the data from buffers ie after reading the data input buffer flush the input buffer ie scanf(" %d", &led); 它对我来说在代码块中工作正常。...刷新缓冲区中的所有数据,即在读取数据输入缓冲区后刷新输入缓冲区,即scanf(“%d”,&led); fflush(stdin); fflush(标准输入); by this, it will clears the input buffer after reading the data. 这样,它将在读取数据后清除输入缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败#2-变量&#39;input&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;tempID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'tempID' was corrupted 运行时检查失败#2-变量&#39;name&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'name' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted 运行时检查失败 #2 - 变量“数组”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted 运行时检查失败#2-变量&#39;d&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'd' was corrupted 运行时检查失败#2-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM