0

I'm Jitesh a programmer trying to involve in C programming. I'm having a problem in initializing arrays in C.

#include <conio.h>

void disp( char ch)
{
   printf("%c", ch);
}
int main(){
    char arr[10];
    int count = 0, iterator = 0;
    for(int iterator=0; iterator<10; iterator++){
        printf("\nAlphabet %d:", count + 1);
        scanf("%c", &arr[iterator]);
    }
   for (int x=0; x<10; x++)
   {
       
       /* Iā€™m passing each element one by one using subscript*/
       disp (arr[x]);
   }

   return 0;
}

I'm trying to input the variable in the array and print out the array. The output should look like this.

Alphabet 1: A //A is inputted variable
Alphabet 2: B
Alphabet 3: C
Alphabet 4: A
Alphabet 5: C
Alphabet 6: A
Alphabet 7: B
Alphabet 8: C
Alphabet 9: A
Alphabet 10: C

A B C A C A B C A C

Do help me out by providing the full solution

  • 1
    You are typing in `"A\n"` and `scanf("%c", &arr[iterator]);` only reads the `A`. The next `scanf` will then read the `\n` which is not what you want. Change to `scanf(" %c", &arr[iterator]);` as explained in the duplicate post. ā€“ kaylum Nov 30 '20 at 05:27
  • If that is not the problem then please explain more clearly what exact problem you are observing. That is, you have given the expected result but you have not actually described what problem or incorrect behaviour you are asking about ā€“ kaylum Nov 30 '20 at 05:28

0 Answers0