c - How to make sure the user enter a positive number -


i did this, sadly not working, , crashing when input char/string, etc.

how fix it?

while((scanf("%d",&numofdef) != 1 ) && (numofdef>0 )) {   printf("not pos num try again"); } 

actually, 2 changes need made: while((scanf("%d",&numofdef) >= 0 ) && !( numofdef>0 ))

firstly, compare return value of scanf 0, secondly condition should either !( numofdef>0 ) or ( numofdef<=0 )

return value of scanf(source: http://www.geeksforgeeks.org/g-fact-10/) :

in c, printf() returns number of characters written on output , scanf() returns number of items read.

ideone link code: http://ideone.com/tjtudy


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -