scanf and gets

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
44557FW
New poster
Posts: 2
Joined: Wed Apr 07, 2004 4:34 am

scanf and gets

Post by 44557FW »

i just changed the following piece of code

[cpp]gets(a);
if(a[0]=='#')
break;
gets(b);[/cpp]
by this
[cpp]scanf("%[^\n]\n",a);
if(a[0]=='#')
break;
scanf("%[^\n]\n",b);[/cpp]
first one was AC(problem 10192) but the other one ran for more than a second an it got Output Limit Exceeded. Both of them ran well in my pc.
I have used scanf like above in other problem and it was ok.Any idea of why is this happening?
abishek
Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

Re: scanf and gets

Post by abishek »

44557FW wrote: [cpp]scanf("%[^\n]\n",a);
[/cpp]
may be it is interpreted as any string not containing a '\' and a 'n'?
44557FW
New poster
Posts: 2
Joined: Wed Apr 07, 2004 4:34 am

Post by 44557FW »

thanks to abishek for replying, i realized that scanf used in that way ignore lines where there are only spaces, so i'll keep using gets.
Post Reply

Return to “C++”