1

This is really strange it prints this line printf("Do you want to continue Yes (Y) or No (N): \n"); Not using any loop nothing but still it prints that statement twice

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");
        }

        led = 0;
        ohm = 0;
        printf("Do you want to continue Yes (Y) or No (N): \n");
        scanf("%c", &check);

        if(check =='Y' || check =='y')
        {

            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;

}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
user2625486
  • 157
  • 2
  • 5
  • 13
  • 5
    "Not using any loop" - there are several loops in your code... – Oliver Charlesworth Jul 29 '13 at 06:50
  • 1
    Looks familiar... http://stackoverflow.com/questions/17917029/run-time-check-failure-2-stack-around-the-variable-check-was-corrupted I meant it when I said to avoid using `scanf`. – jamesdlin Jul 29 '13 at 06:56
  • 1
    Someone needs to read up about `else`. And functions. And not using their output string as the first arg to `printf`. – cHao Jul 29 '13 at 06:57

5 Answers5

3

The first time scanf("%c", &check); finds some garbage (that does not match y or n) so your program can do another loop.

As someone else already noticed, that garbage might be the newline after the ohm input.

Some ideas to solve the problem:
http://www.velocityreviews.com/forums/t436518-get-rid-of-trailing-newline-for-scanf.html

scanf(" %c", &input);

(leading space will eat whitespace in the input)


scanf() leaves the new line char in the buffer

Antonio
  • 19,451
  • 13
  • 99
  • 197
3

Use scanf(" %d", &led); , scanf(" %c", &check); etc. in the code. Adding an extra space before the format specifier will solve the problems caused by garbage/newline in the buffer.

Allen Job
  • 46
  • 2
0
scanf("%d", &ohm);

Here when you input a number and press ENTER, the number is processed by scanf, but the newline is still in the input buffer, then later

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

check will actually store the newline, not 'N',

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

so flag will not be set to 1, the while loop will continue again. In the second loop, check can be assigned 'N', the loop will terminate after.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

Please try scanf("%c ", &check);. Notice the extra space given after %c which will absorb extra new line character.

Dayal rai
  • 6,548
  • 22
  • 29
0

use getchar and it works just fine, you can try it here

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70