0

Can anybody tell me why the loop does not exit whenever I press letter X? How to make the program not get the value of backspace and enter into the array?

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#define N 2
#define M 4


int main()
{
int i,j,a[N][M];

for(i=0;i<N;i++)
{
    for(j=0;j<M;j++)
    {
        scanf("%c",&a[i][j]);
        if(a[i][j]=='X')
            break;
    }
        if(a[i][j]=='X')
            break;
}
    return 0;
}
Dave Nguyen
  • 129
  • 1
  • 1
  • 8
  • 1
    If you are passing input via stdin on a terminal, then you must press ENTER after pressing 'X", only then the stdin buffer will be passed to your program. – Nishant Aug 11 '16 at 07:08
  • please change the title! your problem is about scanf, nothing wrong with the loop commands. – ralfg Aug 11 '16 at 07:36
  • [How to read / parse input in C? The FAQ](http://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – Lundin Aug 11 '16 at 08:02

4 Answers4

2

Change scanf("%c",&a[i][j]); to scanf(" %c",&a[i][j]);

This allows for any spaces to be bypassed before scanning the character.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Why does this happen? – Dave Nguyen Aug 11 '16 at 07:18
  • 1
    @DaveNguyen It is needed as `'\n'` remains in `stdin` because of previous `scanf` . So , next time `scanf` reads `'\n'` and returns . Therfore, you need to put space before `'%c'` . – ameyCU Aug 11 '16 at 07:28
  • 1
    This is how the input buffer(STDIN) operates. Any character that is pressed goes into the buffer. Generally, %c will ignore spaces before the character, but if you press enter, then it will take that as an input and store `\n` – Rishikesh Raje Aug 11 '16 at 07:29
1

There are two problems in your code:

  • The first one is already pointed out by Rishikesh Raje: You need to add a space to the scanf() command in order to eat up the scanned "\n" characters.

  • Then, you scan characters (%c) and try to store them in an int-array. Use

    char a[N][M];
    

    instead. My gcc gives a warning at your erroneous code. Other compilers may silently ignore this.

    Still, in an little-endian-environment (Like PC's) one could think: a char stored at the address of an int-variable should result in the same value. However, the char-value occupies only one byte, the remaining bytes (3 or more) keep uninitialized. If there were zero-bytes before, than a[i][j] will be 'X', otherwise, it will be some random number.

    This explains the behaviour, I think you observed: The program stopped randomly at some 'X' but not always.

ralfg
  • 552
  • 2
  • 11
0

Change the type of array a from int to char

int i,j,a[N][M];

change to

int i,j;
char a[N][M];

Usually gcc would warn you too for this:

so.c: In function ‘main’:
so.c:16:9: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
         scanf("%c",&a[i][j]);
Anshul
  • 1,416
  • 1
  • 16
  • 42
0

Make it an char array instead of an int array

char a[N][M];

Instead of,

int a[N][M];

Anand Kumar
  • 169
  • 1
  • 11