You are not logged in. Please login at www.codechef.com to post your questions!

×

Code being accepted with scanf but not with gets

These are the two solutions i tried submitting today- https://www.codechef.com/viewsolution/19063316- THE CORRECT ONE, https://www.codechef.com/viewsolution/19063318- THE INCORRECT ONE the only difference between the two is that in one of the codes i have used scanf() to read the string while in the other one i have used gets(). Codechef is not accepting the code in which i have used gets() . Can somebody please tell why?

asked 04 Jul '18, 09:16

shaggyaggr's gravatar image

2★shaggyaggr
32
accept rate: 0%


Change the line where you are taking input for len including a newline character match.

From --- scanf("%d",&len); To --- scanf("%d\n",&len);

link

answered 04 Jul '18, 09:47

ionstring's gravatar image

3★ionstring
974
accept rate: 20%

Yes, that worked. Thank you so much, but i still haven't understood the logic behind it. Kindly explain the logic also. Infact, this piece of code is giving wrong output over my ide but codeblocks has accepted it!

(04 Jul '18, 12:47) shaggyaggr2★

Little bit of theory first:

  1. When you use gets() function [ Note:- avoid using it its deprecated] it take the complete string until it reaches EOF or "\n" while in case of scanf it reads upto any whitespaces.
  2. When you mentioned "\n" in scanf while reading it removes the whitespaces characters until it reach any non-whitespace character.
(04 Jul '18, 13:35) ionstring3★

So, when you used only scanf to read your len variable it reads upto the integer value in the input buffer and when you called gets function it start reading from next character onwards which is of course new line character in input stream so gets() match newline character and store it in your string value instead of your actual string what you wanted.

While in case of scanf with newline character at last eats up all the whitespaces after the integer value and then when gets() read it reads your actual string.

Sorry to comment in two parts but because of character limits i did so.

(04 Jul '18, 13:35) ionstring3★

I am sorry to bother you again and again with my stupid questions but my code flushes the buffer before accepting string using gets() ,so how's it considering the newline charctr to be the string. Also in my previous commnt i meant codechef and not codeblocks. My code after the suggested modification is correct according to codechef only otherwise codeblocks is giving wrong output with that code. Why's this happening?

(04 Jul '18, 16:43) shaggyaggr2★

flush() should be used with output stream only as using it with input stream is undefined behaviour. While some compilers will perform as expected in your code but not all, if you were using microsoft visual studio it might be working but better is to avoid all the things having undefined behaviour in C/C++.

(04 Jul '18, 17:42) ionstring3★

https://www.youtube.com/watch?v=N9SAeHSjK00 . At 18:22 he has used fflush with input stream. fflush is used to clean the input buffer if i am corrrect?

(04 Jul '18, 20:09) shaggyaggr2★

Instead of sticking to this tutorial better to look at documentation. Please follow the link below it is from C++ but the function is same for C.

http://www.cplusplus.com/reference/cstdio/fflush/

It clearly mentions that behaviour is implementation dependent for input streams.

Even if you want specific answer to your question check out link below.

http://c-faq.com/stdio/stdinflush.html

ADVICE :- If its not very important to stick with C please get into C++ it will help in CP more than C because of its STL.

(04 Jul '18, 20:52) ionstring3★

okay one last question. Why am i not getting the correct output on codeblocks after the modification you suggested?

(05 Jul '18, 08:26) shaggyaggr2★

To that i can't say much i don't use codeblocks and i tried your code on ideone and on linux it works fine but you should try debugging your code with the inbuilt codeblocks debugger and try to check every input variable. You can also use gdb for debugging or even you can simply use print statements to check what your variables actually contain.

If you use any other compiler setup for codeblock instead of gcc it might be because of that.

If you are unable to find out the problem let me know i will install and check codeblocks.

(05 Jul '18, 10:00) ionstring3★
1

OK thank you so much.

(05 Jul '18, 17:47) shaggyaggr2★
showing 5 of 10 show all
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×643
×40
×8

question asked: 04 Jul '18, 09:16

question was seen: 150 times

last updated: 05 Jul '18, 17:47