Thread: How to test for a char when reading and int on a loop

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    158

    How to test for a char when reading and int on a loop

    I normally use a code like this to read and int between some range:
    Code:
    do {
    		printf("Integer: ");
    		scanf("%d%*c", &value);
    	} while(value < 1 || value > 10);
    But suppose I input something not a digit... this will go into a loop printing "Integer :" and will not end until I Ctrl+C or Ctrl+Z. How can I ask the user to input again if he did not inputed a digit on the first place?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Read as a string and convert.
    http://www.daniweb.com/code/snippet266.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    thanks, that helped alot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  5. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM