Error Reporting

This is a discussion on Error Reporting within the C Programming forums, part of the General Programming Boards category; I have a minor problem i want to check for error that the user might enter. For example if i ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    31

    Exclamation Error Reporting

    I have a minor problem i want to check for error that the user might enter. For example if i ask for a number and the used enters a letter it would prompt saying that the entered crap here's my code:

    Code:
    int emp_details()
    {
         FILE *fp;
         fp=fopen("EMP_DETAILS.txt", "a+");
    
            char emp_name[100];
            char address[100];
            char email[100];
            int tel;
            int age;
            int trn;
            int v_id;
    
         printf("\n\t\t\t****Employee Information Screen****\n\n\n");
    
    
         printf("Enter Employee's Name: ");
         scanf(" %[^\n]s ",&emp_name);
         printf("\n----------------------------------------------------\n");
         printf("\nPlease Enter Employee's Address: ");
         scanf(" %[^\n]s ",&address);
         printf("\n----------------------------------------------------\n");
         printf("\nPlease Enter Employee's Email: ");
         scanf("%s",&email);
         printf("\n----------------------------------------------------\n");
         printf("\nPlease Enter Employee's Telephone #: ");
         scanf("%d",&tel);
         printf("\n----------------------------------------------------\n");
         printf("\nPlease Enter Emplyee's Age: ");
         scanf("%d",&age);
         printf("\n----------------------------------------------------\n");
         printf("\nPlease Enter Employee's TRN#: ");
         scanf("%d",&trn);
    
    
                fprintf(fp,"----------------------------------------------------\n");
                fprintf(fp,"Employee Information Generated on: %s", __DATE__);
                fprintf(fp,"\n----------------------------------------------------\n");
                fprintf(fp, "Employee's Name Is: %s\n", emp_name);
                fprintf(fp,"\nEmployee's Address: %s\n", address);
                fprintf(fp,"\nEmployee's Email: %s\n",email);
                fprintf(fp,"\nEmployee's Telephone#: %d\n",tel);
                fprintf(fp,"\nEmployee's Age: %d\n",age);
                fprintf(fp,"\nEmployee's TRN#: %d\n",trn);
                fprintf(fp,"\n----------------------------------------------------\n\n");
    
         fclose(fp);
    
         system("CLS");
    
                printf("\n\n\tInformation Processed... Press Any Key To Return To Main Menu");
    
         getch();
    
        system( "CLS");
    
         first_menu();
    
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by teensicle View Post
    I have a minor problem i want to check for error that the user might enter. For example if i ask for a number and the used enters a letter it would prompt saying that the entered crap
    The best idea would be to write a set of functions, one that tries to get a string, one that tries to get a number, etc. Then you can re-use those functions the same way you use scanf() over and over.

    Internally, the function could use scanf. scanf()'s return value indicates success and failure. If you want it to grab a number and there is no number to grab, it will return 0.

    But you also want to dispose of the incorrect data. So the function should work like this (pseudocode):
    Code:
    char *get_a_string () {   
         - first, read a line into a buffer with fgets().
         - apply sscanf to the buffer:
              if we fail, return NULL
              if we succeed, return the string
    You probably want to flesh this out a bit, but that is the general idea -- you absord the input first with fgets, then you "parse" it using sscanf(). Notice, two ss, sscanf(), not scanf(). Look it up
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    31
    SOMETHING LIKE THIS?
    Code:
    printf("Enter Employee's Name: ");
       fflush(stdout);
       if ( fgets(emp_name, sizeof emp_name, stdin) )
       {
          char *newline = strchr(emp_name, '\n'); /* check for trailing '\n' */
          if ( newline )
          {
             *newline =  '\0'; /* overwrite the '\n' with a terminating null */
          }
       }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21