STRANGE problem in c

Buy1-Get1 like in many code chef problems ,this problem asks to input number of test case as first line… so i wrote…

t=0; 
while((c=getchar())!='\n'&&c!=EOF) 
{ 
    t=t*10+c-'0'; 
}

id 1762981
this runs on ideone but gives WA in code chef
so i wrote id 1762967

t=0;
scanf("%d\t",&t);

and now it got accepted… :slight_smile:
BUT WHY THIS HAPPENED?

If you want to perform such low level programming, you should handle all special cases…

Does the problem statement specify the line ending?

You can try, if your read functions reads 7474 from following program

#include <cstdio>
int main() {
    printf( "7474\r\n" );
    return 0;
}

:wink:

You are using c!=EOF it does not need here because you have already used c!=’\n’ but better way you can use c>=‘0’…you can do following

 while((c=getc_(stdin))<‘0’){}
t=c;
while(c>=‘0’)
{
t=t*10+c-‘0’;
c=getc(stdin);
}

hope that it is better…
2 Likes
  • scanf("%d\t",&t); //the ‘/’ was missing in question typo…