### div24ever's blog

By div24ever, history, 4 years ago, ,

I am trying to solve Niceness of the string but i am getting WA because i think i am unable to process blank lines. For blank lines output will be zero. I am using scanf(" %[^\n]s",a) which will ignore blank lines.

My code

• -1

 » 4 years ago, # |   0 std::getline()
•  » » 4 years ago, # ^ |   +1 Thank you! getline will behave weirdly if we don't ignore \n. So i came across this. Putting cin.ignore() after scanf("%d",&T) and using getline() gave AC.
•  » » » 4 years ago, # ^ |   +9 There is no weirdness in getline: size_t n; cin >> n; string s; getline(cin, s); // read pointer is just before the end of line, so the empty line is returned after number reading for (size_t i = 0; i < n; i++) { getline(cin, s); // works, as expected } 
•  » » » » 4 years ago, # ^ |   +1 Got it. By adding extra getline before taking string as input we can ignore \n.
 » 4 years ago, # | ← Rev. 3 →   0 Replacing scanf("%s",a) with gets(a) may help. But remember to getchar() after scanf("%d",&T) to skip the '\n' in the first line.
•  » » 4 years ago, # ^ |   +1 Looks like getchar works a bit faster than cin.ignore(). Thanks!
•  » » 4 years ago, # ^ |   +1 If you want to skip '\n', you can write: scanf("%d\n",&T). But this works with bugs when you debug your program (if you are not reading from the file).
 » 4 years ago, # |   0 scanf is for queers