-2

Here is my code that take count as input and then takes that number of cities as input.
I want to print all the cities starting from that letter but program ends before taking the character as input. But it doesn't take character input

#include <stdio.h>
void main()
{

char a[20][10];
char ch;
int i,n;

printf("Enter nos of cities\n");
scanf("%d",&n);
for ( i = 0; i < n ; i++)
{
    scanf("%s",a[i]);
}
printf("\n");

printf("Enter 1st character\n");
scanf("%c", &ch);
for ( i = 0; i < n ; i++)
    if(ch==a[i][0])
        printf("%s\n",a[i]);    


}

OUPUT:

Enter nos of cities
3
asd
zxc
qw

Enter 1st character
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Siddhesh Kand
  • 27
  • 1
  • 5
  • Thank got the answer – Siddhesh Kand Apr 16 '17 at 12:57
  • 1
    -1 for not researching the problem. There are TONS of duplicates of this one on SO. In addition to the solution for your problem, (1) Use the standard `int main(void)` instead of `void main()`. (2) Check if `n` is not greater than 20. (3) Limit the input of `scanf` to avoid buffer overruns: `%9s` (4) Check `scanf`'s return value. – Spikatrix Apr 16 '17 at 13:03
  • 'line feed in buffer' is the new 'i++ + ++i' :(( – ThingyWotsit Apr 16 '17 at 13:08

2 Answers2

5

Use scanf(" %c", &ch);.

The enter from the previous input is taken as input. So add a space before %c.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Sniper
  • 1,428
  • 1
  • 12
  • 28
0

Just add a line getchar(); after printing the newline in line 15 Because that printf("\n") feeds the scanf with a newline character. getchar() skips (eats) the newline character here.