-2

This is a simple program for getting six character from user and storing it in an array and then to display it.But it is not doing so,the print statement in reading section is printed twice.please help me to solve

#include<stdio.h>

int main()
{
    int i=0;
    char a[6];

    for(i=0 ; i<6 ;i++)
    {
        printf("Enter a character \n");
        scanf("%c",&a[i]);  

    }
    for(i=0 ; i<6 ;i++)
    {
        printf("%c",a[i]);  

    }


    return 0;
  • 1
    Possible duplicate of [instructions after scanf() call get called twice](https://stackoverflow.com/questions/46797239/instructions-after-scanf-call-get-called-twice) – Yunnosch Sep 09 '18 at 06:40
  • Do you type input like this: `a` ENTER `b` ENTER `c` ….. and so on ? – Support Ukraine Sep 09 '18 at 06:40
  • The only reason to use `scanf()` instead of `getchar()` to read a single character is explained by what a space in the input format means. Read up on it. – Shawn Sep 09 '18 at 06:40
  • scanf() is complicated. Really complicated. Read the description (e.g. https://en.cppreference.com/w/c/io/fscanf ), follow tutorials. Try not to use it, http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html There are at least 2 scanf questions here per day. 10 in septembers. – Yunnosch Sep 09 '18 at 06:42
  • Dear 4386427 i typed as you said but the statement "Enter a character" prints twice. – Super Cool Bucket Sep 09 '18 at 06:44
  • the output is coming like this : Enter a character a Enter a character Enter a character b Enter a character Enter a character c Enter a character a b c – Super Cool Bucket Sep 09 '18 at 06:47
  • Please show the output. Explain whether for the six keystrokes "a enter b enter c enter" you get twelve outputs or six. Do so by editing your question, not by adding comments. – Yunnosch Sep 09 '18 at 06:47
  • @SuperCoolBucket Then what happens is that you first get an `a` and then you get the NEWLINE (i.e. `'\n'` character). In other words - two characters instead of one. – Support Ukraine Sep 09 '18 at 06:48
  • Please explain how much of the proposed duplicate (linked by a helpful user in the first commment on this quesiton) is helpful for your problem or why it is not helpful. – Yunnosch Sep 09 '18 at 06:49
  • Sir even if i remove \n it is not solving problem. kindly refer to the statement "printf("Enter a character");" This statement is executing twice and only one character is read by scanf. – Super Cool Bucket Sep 09 '18 at 06:51
  • @SuperCoolBucket How do you "remove \n" ? – Support Ukraine Sep 09 '18 at 06:52
  • I got the answer : as scanf is taking the enter as another input that problem is occuring. so to solve just add a "getchar()" after scanf. – Super Cool Bucket Sep 09 '18 at 07:35
  • Or better, just provide a `space` before the *conversion specifier* in your *format-string* so that `scanf` will consume *leading-whitespace* (which includes `'\n'`), for instance using `scanf(" %c",&a[i]);` (note the additional space and the `scanf` **return** should be checked for `EOF` even when reading characters) – David C. Rankin Sep 09 '18 at 08:45
  • as said above, add a space in your scanf line, like this: `scanf(" %c",&a[i]); ` – George Bou Sep 09 '18 at 08:49
  • Sir can you tell me why scanf consumes extra spaces or \n. I mean we have to read a character from the monitor and to store it in a variable whose address is &a[i] .According to my small program after reading the character the loop should repeat but scanf is taking more characters!!!!. I will be glad if you kindly reply. – Super Cool Bucket Sep 10 '18 at 09:19

1 Answers1

-3

you made a mistake when importing the input, you can use %s

#include<stdio.h>

int main()
{
    int i=0;
    char a[6];
    for(i=0;i<6;i++)
    {
        printf("%d-)Enter a character : ",i+1); scanf("%s",&a[i]);
    }
    for(i=0;i<6;i++)
    {
        printf("Value of %d. element in Array : %c\n",i+1,a[i]);
    }

    return 1903; //Beşiktaş Football Club was founded in 1903 :)
}
  • This is completely wrong. OP wants to put character inputs in an array, not input a string. In any case, you should always specify a maximum width when using the `%s` conversion specifier to avoid buffer overflow. But, since `scanf()` always terminates a string with `\0`, this code is _guaranteed_ to write past the end of the array on the last input loop execution, causing undefined behavior. – ad absurdum Sep 27 '18 at 12:41