C-programming loop wont stop with scanf!=0 -
what wrong ? also, have use scanf(). supposed read integers , sum them, loop stop when 0 entered..
main (void){ int a; int r=0; while(scanf(" %d",&a)){ r=r+a; } printf("the sum %d\n",r); return 0; }
quoting man
these functions return number of input items assigned. can fewer provided for, or zero, in event of matching fail- ure. 0 indicates that, although there input available, no conver- sions assigned; typically due invalid input character, such alphabetic character `%d' conversion.
value eof returned if input failure occurs before conversion such end- of-file occurs. if error or end-of-file occurs after conversion has begun, number of conversions completed returned.
so, pretty explains returned scanf().
you can solve problem adding ( 1 == scanf("%d", &a) && != 0 ) condition in while loop like
int main (void) { int a; int r=0; while( 1 == scanf("%d", &a) && != 0 ) { r=r+a; } printf("the sum %d\n",r); return 0; } also note have specify type of main int main().
i add loop end when enter character 'c' ( or string ) , show sum of numbers entered before entering character.
Comments
Post a Comment