Thread: Character function not returning

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    34

    Character function not returning

    Hello,
    I'm compiling with Dev-c++ 4.01. This routine accepts
    six alphanumeric characters, e.g. A1B2C3, and is
    part of a mailing list program.

    The program compiles without errors. When run it
    seems to skip over the character functions (no input),
    and prints the error handling statement:"The ZIPcode
    must be alphanumeric only."

    What am I doing wrong? Am I omitting something,
    or is the design faulty?

    This is what I'm able to do.

    Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    /*Prototype*/
    
    char err_msg(char msg[]);
    char getzip(char zipcode[]);
    int main();
    
    
    char err_msg(char msg[])  {
    printf("\n\n%s\n", msg);
    fflush(stdout);
    return;
    }
    char getzip(char zipcode[])
    {
          int ctr;
          for(ctr = 0; ctr < 6; ctr++)
         {
              zipcode [ctr] = toupper(zipcode [ctr]);
                   if(! isalpha(zipcode [ctr]))
                   {
                         if(! isdigit(zipcode[ctr]))
                            {continue;}
                       else
                             {
                                  err_msg("The ZIPcode must be alphanumeric only");
                                 break;
                             }
                     }
             }
     }
    int main()
    {
         int ch;
         char zipcode[7];          /*Extra null character.*/
    
         do
             {
                  ch = 0;
    
                  printf("Enter ZIPcode");
                  fgets(zipcode, 7, stdin);
                  while(getchar() != '\n');
                   ch = 1;
             }
                   while(ch);
                   system("PAUSE");
                   return;
        }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if(! isdigit(zipcode[ctr]))
    Are you sure the above line is correct. As it stands now, if your zipcode contains numbers, it will go to the error message.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Adock
    This routine accepts six alphanumeric characters, e.g. A1B2C3
    Why not use isalnum?
    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.*

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Adock
    Hello,
    I'm compiling with Dev-c++ 4.01. This routine accepts
    six alphanumeric characters, e.g. A1B2C3, and is
    part of a mailing list program.

    The program compiles without errors. When run it
    seems to skip over the character functions
    (no input),
    and prints the error handling statement:"The ZIPcode
    must be alphanumeric only."

    What am I doing wrong? Am I omitting something,
    or is the design faulty?

    This is what I'm able to do.

    Thank you.

    Code:
    int main()
    {
         int ch;
         char zipcode[7];          /*Extra null character.*/
    
         do
             {
                  ch = 0;
    
                  printf("Enter ZIPcode");
                  fgets(zipcode, 7, stdin);
                  while(getchar() != '\n');
                   ch = 1;
             }
                   while(ch);
                   system("PAUSE");
                   return;
        }

    Ummm ... you never call getzip() from main(). What am I missing here? How does it print the error message? How does it ever leave the do{...}while(ch) loop? (Ctrl-c, I guess --- ok for debug purposes, but makes everything after the loop superfluous.)

    Dave
    Last edited by Dave Evans; 09-24-2004 at 01:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM