-1

this program with this value : ABCDEF is ok at the frist

but again when you enter a value with space,like : ABC DEF the program works wrong!!!! while loop ignores scanf the second time What am I doing wrong?!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(){
    bool checkSrc = false;
    bool checkDst = false;

    while (!checkSrc && !checkDst)

    {

    char ins[10];

    printf("White Player : ");
    scanf("%s",&ins);

    }

}
SWIIWII
  • 387
  • 5
  • 15
  • 5
    The `scanf` format `"%s"` reads *space delimited* "words". – Some programmer dude Jun 08 '16 at 11:55
  • @user3121023 Yes, although it is also necessary to check for a `'\n'` character at `ins[strlen(ins)-1]`. If it is not there, the buffer was too small and there are unread characters remaining on the input line. If it is there, the buffer was large enough and you can do further manipulation on the string, such as stripping leading and trailing whitespace characters, including the final newline character. – Ian Abbott Jun 08 '16 at 12:08

1 Answers1

2

%s - String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

I recommend you using fgets() instead of scanf() since the latter has no buffer overflow protection.

#define namesize 15
char *ins = malloc (namesize);

fgets(ins, namesize, stdin);
Mirakurun
  • 4,859
  • 5
  • 16
  • 32