0

I am creating a C program using structure in which I take input of 10 employee information and print it. I'm using for loop for taking input.

But I'm unable to take the name of the next employee if I use space in between, like this:- JAMES WILLIAM (Imaginary name), But if I write JAMES or JAMES_WILLIAM it will take input of the next employee name. I have tried scanf("%s", &emp[i].name) and gets(emp[i].name) but the same problem is repeating. You can find my entire program here.

Would greatly appreciate any tips! Thanks for the help!

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Yashraj2502
  • 1
  • 1
  • 1
  • `scanf()` stops reading on whitspace. `gets()` will stop reading on newline, thus it can read in values that have whitespace in them, but [it is dangerous](https://www.geeksforgeeks.org/gets-is-risky-to-use/) to use. Use `fgets()` or `getline()` or equivalent instead. – Remy Lebeau Aug 24 '21 at 17:50
  • 1
    While `scanf` is okay for simple things when starting out, once things start to get a bit more complicated it's not really enough by itself. As mentioned the POSIX (Linux and macOS) function [`getline`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) is a good way to read a line. Or the C standard [`fgets`](https://en.cppreference.com/w/c/io/fgets) function. – Some programmer dude Aug 24 '21 at 17:52
  • 1
    Don't use `gets`. It's broken by design and dangerous. – Cheatah Aug 24 '21 at 18:20
  • While you should not use `gets`, it should have read a full line, spaces and all. So it should definitely not have the same problem as the `%s` format for `scanf`. *Unless* you enter one name per line, in which case you need to read two (or more) times. – Some programmer dude Aug 25 '21 at 15:08

0 Answers0