Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alphanumeric zipcode

Status
Not open for further replies.

DocHaydn

Programmer
Feb 24, 2004
12
CA
Hello,
I'm compiling with Dev-c++ 4.01.
I'm trying to capture a six digit alphanumeric zipcode
with this routine; which comes from a mailing list program.
The compiler is reporting one error:"Parse error at end of
input." I must be missing a curly brace or two, but i can't
find where to put them.

I'm aware of other problems in the program, e.g. true/false
statements; but for now I just want this program to compile
and run.
Here's my code:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

/*Prototype*/

char err_msg(char err_msg[]);
char getzip(char zipcode[]);
int main();

int main()
{
    char err_msg(char err_msg[];
    printf("\n\n%s\n", err_msg);
    fflush(stdout);

    return;
}
char getzip(char zipcode[])
{
    int ctr, ch, false, true;
    char getzip(char zipcode[7]); /*extra null character.*/

    do
      {
         ch = 0;
         printf("What is the ZIPcode?");
         fgets(zipcode, 7, stdin);
         for(ctr = 0; ctr < 6; ctr ++)
         {
             zipcode[ctr] = toupper(zipcode [ctr]);
             if((ctr % 2)== 0)
             {
                 if(! isalpha(zipcode [ctr]))
                 {
                    return false;
                 }
                 else
                 {
                     if(! isdigit(zipcode [ctr]))
                     {
                        return false;
                     }
                        return true;
                  }
               }
               else
               {
                   err_msg("The ZIPcode must be alphanumeric only");
                   ch = 1;
                   break;
               }
            }
                while (ch);
                
                system("PAUSE");
                 
                return;
         }
 
Well, there's quite a bit going on here, so I'll just throw some comments in to point out what's going on.

I put in the proper indentation to make it look a little better.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

/*Prototype*/

char err_msg(char err_msg[]);  /* not a good idea for the parameter to have the same name as the function */
char getzip(char zipcode[]);
int main();

int main()  /* based on context, this looks like your err_msg function, not main */
/*char err_msg(char err_msg[])*/  /* this is what you should have */
{
  char err_msg(char err_msg[];  /* not sure what this is for... this line goes */
  printf("\n\n%s\n", err_msg);
  fflush(stdout);

  return;
}

char getzip(char zipcode[])  /* this should really be your main. */
{
  int ctr, ch, false, true;
  char getzip(char zipcode[7]);  /* again, not sure what this is for... remove */
  /* actually, char zipcode[7] should be a local variable.  get rid of the function parameter */

  do
  {
    ch = 0;
    printf("What is the ZIPcode?");
    fgets(zipcode, 7, stdin);
    for(ctr = 0; ctr < 6; ctr ++)
    {
      zipcode[ctr] = toupper(zipcode [ctr]);
      if((ctr % 2)== 0)  /* not sure why you're only looking at every other character.  this should go  */
      {  /* this goes along with the above, and is part of your missing curly brace problem */
      /* calling return is not what you want in the following lines.  
         doing so will take you out of function.  you should have this:  */
      /*
      if(! isalpha(zipcode [ctr]))
      {
        if(! isdigit(zipcode [ctr]))
        {
          err_msg("The ZIPcode must be alphanumeric only");
          ch = 1;
          break;
        }
      }
      */
      if(! isalpha(zipcode [ctr]))
      {
        return false;
      }
      else
      {
        if(! isdigit(zipcode [ctr]))
        {
          return false;
        }
        return true;
      }
      }
      else
      {
        err_msg("The ZIPcode must be alphanumeric only");
        ch = 1;
        break;
      }
    /* you need a } here */
  }
  while (ch);

  system("PAUSE");

  return;
}
 
There are yet another not a good idea(s) in this code, i.e.:
1. Don't use names true and false for your own variables. This names reserved in C++, don't lose compatibility if no matter. But these variables was not initialized in the snippet!..
2.If statements cascade is not well designed. There is yet another good macros/function in <ctype.h>: isalnum(). Try redesign this cascade logic and you can write one simplest if only...
3. Don't write in the (old Fortran?) style, you are in C; monstrous construct:
Code:
if(! isdigit(zipcode [ctr]))
{
   return false;
}
return true;
is exactly the same as:
Code:
return isdigit(zipcode[ctr]);
Remember the main rule: Keep it simple...
 
Thank you all for the education.

I'm very impressed.

Thank you,
Doc Haydn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top