Why does my loop skip even steps?

Aditya
#include<stdio.h>

int main()
{
char a[10];
for(int i=0; i<5; i++)
{
   printf("\nEnter a character: ");
   scanf("%c",&a[i]);
}

}

In this loop, the program first asks the question normally. But in the second loop the program doesn't give me option to input a character and immediately runs the third loop.

Likewise, all the even iterations are skipped.

Mihir Kale

I agree with @Keine

You should be able to get the expected output by adding a space before the %

Corrected code

#include<stdio.h>

int main()
{
char a[10];
for(int i=0; i<5; i++)
{
   printf("\nEnter a character: ");
   scanf(" %c",&a[i]);
}

}

SOURCE https://gsamaras.wordpress.com/code/caution-when-reading-char-with-scanf-c/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the code skip the for-loop

From Dev

why does javascript skip some of my functions?

From Dev

Why does `i` become a string in my loop?

From Dev

Why does "font-weight: bolder" skip boldness steps?

From Dev

Why does Angularjs hate my while loop?

From Dev

Why my foreach loop not failing even if the Path is wrong

From Dev

Why does my if statement create an infinite loop?

From Dev

Why does my code run into an infinite loop?

From Dev

My step counter does not reset the steps, even after uninstall

From Dev

Why does my program loop twice?

From Dev

Why does this @JsonIgnore fix my infinite loop?

From Dev

Why does my pandas filters work in separate steps but not in one command?

From Dev

Why does this loop in my decryption function not work?

From Dev

Why does Mockito skip the initialization of the member variable of my abstract class

From Dev

Why does uint break my for loop?

From Dev

why does javascript skip some of my functions?

From Dev

Why does my loop rotate only once?

From Dev

Why does Xcode skip for loop code?

From Dev

Why does my for loop come once?

From Dev

Why does my for loop skip my last values in my array?

From Dev

why does my loop only run once?

From Dev

Why does my call to an MVS POST function skip the RedirectToAction?

From Dev

Why does my for loop go out of bounds?

From Dev

Why does this for loop ignore my variable?

From Dev

Why does the compiler skip my second scanf loop?

From Dev

why does my program loop infinitely?

From Dev

why my loop will not stop even after i enter correct

From Dev

Why does my code skip over certain nodes?

From Dev

Why does my loop skip every second list entry?

Related Related

HotTag

Archive