Reputation: 157
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: \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;
}
Upvotes: 0
Views: 3422
Reputation: 7917
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:
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 \n
.
Both if
conditions will fail; check
is neither Y
nor y
, neither N
nor n
.
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.
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() != '\n')
and after each call to scanf()
, type FLUSH;
. It's a lot safer.
Upvotes: 2
Reputation: 1641
Its working fine for me in code-blocks .... flush all the data from buffers i.e after reading the data input buffer flush the input buffer i.e scanf(" %d", &led); fflush(stdin); by this, it will clears the input buffer after reading the data.
Upvotes: 0
Reputation: 11232
scanf("%s", &check);
should be scanf("%c", &check);
as you reading a char
not a string.
Upvotes: 2
Reputation: 2790
The problem is that your variable "check" is too small.
scanf("%1s", check);
Because you are saving a string (with terminating \0) you should use a larger variable:
char check[2];
Edit: But yes, scanf("%c", &check) is far better if input is only one single character.
Upvotes: 1