1
void loginForm() {
    char username[100], password[100];
    printf("Username: ");
    scanf("%[^\n]", username);
    printf("%s", username);
    printf("Password: ");
    scanf("%[^\n]", password);
    printf("%s", password);
}

I've been experiencing issues with this code in VS2015, where everytime I input my username it just goes on havoc, like this:

Username: Tenzo
Password: Tenzo╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Tenzo

I'm confused since in the code, I want to print the "Username" first before asking the user to input their password, but as you can see, I haven't even typed anything to the password but as you can see it already looked like that. Tested the code on VS2010 and everything is working just fine. I don't know what is happening.

NB: The code is written in C, not C++.

  • Try it with a space before the percent sign: `" %[^\n]"`. Also, you don't need the ampersands in front of `username` and `password` in the `scanf`s. – user3386109 Dec 02 '16 at 08:39
  • Oops, sorry that I didn't notice the ampersands. I removed that. And wow, that works! You may want to use the answer mode to reply with more info on what is happening, as I can't set comment as the answer :) – TenzoNakami Dec 02 '16 at 08:42
  • The `scanf` function stops reading on whitespace (like newline). The newline in the input buffer will not be copied into the buffer you provide. So a simple `"%99s"` format should be enough for your `scanf` calls. In fact, using the `"%["` format will lead to problems, because it doesn't skip leading whitespace. So the second `scanf` call will read the newline from the previous call. – Some programmer dude Dec 02 '16 at 08:43
  • @Someprogrammerdude I'm aware of that, but does space counts as whitespace? Since the Username and Password may consist of space also. And also about the second scanf, I do put in fflush(stdin) after each scanf, I just don't show them here, is it enough? – TenzoNakami Dec 02 '16 at 08:44
  • Ah yes space is of course a whitespace character. Then your format string needs to contain a leading space as suggested by @user3386109, to discard leading spaces (like the previous inputs newline). – Some programmer dude Dec 02 '16 at 08:45

1 Answers1

1

The problem is that the "%[^\n]" format specifier will leave the newline in the input buffer. The following scanf won't skip that newline because the %[] format specifier doesn't skip leading whitespace by default. (By contrast, most of the format specifiers like %s, %d and %f do skip whitespace by default.)

To solve the problem, you can put a space at the beginning of the format string, like so

scanf(" %[^\n]", password);
       ^ this space forces scanf to skip any leading whitespace, including newlines
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Thanks for the answer and your time to explain things up! :) Btw, should fflush(stdin) work just fine after each scanf? – TenzoNakami Dec 02 '16 at 09:50
  • @TenzoNakami According to the C specification, `fflush` is only allowed on output streams and update streams (§7.21.5.2). But [Microsoft allows it on input streams](https://stackoverflow.com/a/34247021/3386109), so I leave it up to you to decide. – user3386109 Dec 02 '16 at 10:15
  • 1
    Well, I tried that just now but it doesn't work. Well, I'll just stick with your answer then! :) – TenzoNakami Dec 02 '16 at 11:44