Thread: Error detection

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Error detection

    How to design a function that can check name and number whether it is valid or not? If the name contains symbol and/or number while number contains alphabet and/or symbol, it will prompt the user to key in again the valid input.
    Last edited by ulti-killer; 06-12-2012 at 03:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A loop, and various calls to the likes of isdigit() et al in ctype.h

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      if ( validate(buff) ) {
        // do your thing
      } else {
        // complain
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Why it doesn't work? It shows 2 error about error C1075: end of file found before the left brace '{' and IntelliSense: argument of type "char *" is incompatible with parameter of type "int". The name variable in line 19 has red underline.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    // Function main begins program execution
    int main(void)
    {
    char name [50], *line;
    
        // Prompt and read the user's na5me
        printf("Name: ");
        // Remove newline
        line = strchr(name,'\n');
        *line = '\0';
        fflush(stdin);
        while ( fgets(name, sizeof(name), stdin ) != NULL ) 
        {
            if ( isalpha (name) )
            {
            printf("Name: %s", name);
            }
            else
            {
            printf("Enter name again");
            // Prompt and read the user's name
            printf("Name: ");
            // Remove newline
            line = strchr(name,'\n');
            *line = '\0';
            fflush(stdin);
            }
    
        system ("pause");
        }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    fflush(stdin);
    Why fflush(stdin) is wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error Detection and Correction
    By daisy_polly in forum C Programming
    Replies: 5
    Last Post: 02-22-2006, 06:13 PM
  2. Question regarding RPN and error detection
    By Roule in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 01:12 AM
  3. Data Format Error Detection.
    By Ti22 in forum C Programming
    Replies: 2
    Last Post: 01-03-2004, 11:52 AM
  4. error detection
    By nas in forum C Programming
    Replies: 12
    Last Post: 06-22-2002, 05:14 PM
  5. error detection continued
    By nas in forum C Programming
    Replies: 3
    Last Post: 06-21-2002, 10:35 PM