Re: making sure only integer is input



Radith wrote:
Hi All,

I have a number guessing game in which users try to guess a random number. Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired infinite loop.
Now, I know a string is not what we're after but in terms of error-handling; How can we prevent users from entering a string?
and if we can't do that:
How can we make sure if a string is entered, it is realized and discarded. Implying, that the user is warned about the mistake and prompted again in hope of correct input this time??


I have not included my program here because it is unncessarily large (still a begginer), but if y'all want; more than happy to attach it.

Thanks for all your time in advance.

Here's a function that demonstrates one safe way to read in an integer, and reject invalid input. It reads a line of text and then checks whether it consists solely of digits. If so, it is converted to an integer and if there was no error, the integer is returned. Otherwise, another line is read, and so on.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

long read_integer(void)
{
  /* declare a buffer for holding the input line */
  char buf[100] = "";

  /* output the initial prompt */
  printf("Enter an integer: ");
  fflush(stdout);

  /* while the user inputs a line of text */
  while(fgets(buf, sizeof buf, stdin))
  {
    /* Remove the newline character from the buffer */
    char *p = strchr(buf, '\n');
    if(p) *p = 0;

    /* check if input contains only digits */
    if(strspn(buf, "0123456789") == strlen(buf))
    {
      long result;
      errno = 0;

      /* if no conversion error, then break */
      result = strtol(buf, 0, 10);
      if(errno == 0)
        return result;
    }

    /* Otherwise, output another prompt */
    printf("Invalid input, try again: ");
    fflush(stdout);
  }

  if(feof(stdin))
  {
    printf("End of file reached\n");
    return -1;
  }

  printf("Error reading input\n");
  return -2;
}

If the number returned by this function is negative, some unrecoverable error occurred.

--
Simon.
.



Relevant Pages

  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Secure C library
    ... I read much of the new "security TR", and gee, I don't know. ... the buffer from the buffer size. ... It is not hard to design a better form of buffer and string handling. ... but this is just one example of how thoughtful interface design can ...
    (comp.std.c)
  • Re: Secure C library
    ... >> string functions don't make much sense once you add bounds-checking ... >> designing an interface just for the purpose of reducing the frequency ... > make buffer size decisions more visible, ... Bstrlib is also very interoperable with char *'s, ...
    (comp.std.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you check EVERY return from a call that can fail, ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: Calling dll functions from vb.net with pointer returns!
    ... (ByRef pulLen As Integer, ByVal pszFilter As String, ByVal ulFlags As ... OUT PTCHAR Buffer, ... Address of a buffer to receive a set of NULL-terminated device instance ... pszFilter must specify the name of a device ...
    (microsoft.public.dotnet.languages.vb)

Loading