customer input

This is a discussion on customer input within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a program in C to enter basic information about a customer into several string variables. ...

  1. #1
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12

    customer input

    Hi, I have to write a program in C to enter basic information about a customer into several string variables. This is the criteria I have to follow:
    Write a program in C to enter basic information about a customer into several string variables. Use printf() for screen output and the fixgets() function to input each field. Combine all of the individual strings to one string for output as illustrated. This will require the use of strcat() and strcpy() as demonstrated in class. The field length is listed on the sample screen. Remember that the variable is declared 1 larger to accomodate the NULL. The combined field should be 133+1 characters. The input locations should be in the same column, as illustrated. Also, the char array for each field is one greater than the field length to accommodate the NULL.

    Basic Input Screen (space) Field Length (do not display on screen)
    Customer Number A12345 (space) 6
    First Name James (space) 15
    Middle Initial E (space) 1
    Last Name Jones (space) 25
    Address 234 S. Maple Street (space) 30
    City San Fran (space) 15
    State Ca (space) 2
    Zip Code 12345 (space) 10
    Phone 345-123-4567 (space) 12

    ----------Output--------
    A12345 / Jones, James E / 234 S. Maple Street / San Fran, Ca 12345 / 345-123-4567
    This is what I have:

    Code:
    /* basicinput.c by Aron, 11/21/05 */
    /* This program shows basic information about a customer using strings
     * The strings are copied inton on string at the end */
    
    #include <stdio.h>
    #include <string.h>
    int fixgets(char *, int ,FILE *);
    
    int main ()
    
    {
    /* Declarations */
    char cusnum[7], fname[16], mi[2], lname[26], address[31], city[16], st[3], zcode[11], phone[13];
    char combo[134];
    
    
    /* Input Section */
    printf("Customer Number ");
    fixgets(cusnum, 7, stdin);
    
    printf("\nFirst Name      ");
    fixgets(fname, 16, stdin);
    
    printf("\nMiddle Initial  ");
    fixgets(mi, 2, stdin);
    
    printf("\nLast Name       ");
    fixgets(lname, 26, stdin);
    
    printf("\nAddress         ");
    fixgets(address, 31, stdin);
    
    printf("\nCity            ");
    fixgets(city, 16, stdin);
    
    printf("\nState           ");
    fixgets(st, 3, stdin);
    
    printf("\nZip Code        ");
    fixgets(zcode, 11, stdin);
    
    printf("\nPhone           ");
    fixgets(phone, 13, stdin);
    
    
    /* Processing */
    strcpy(combo, cusnum);
    strcat(combo, " / ");
    strcpy(combo, lname);
    strcat(combo, ", ");
    strcpy(combo, fname);
    strcpy(combo, mi);
    strcat(combo, " / ");
    strcpy(combo, address);
    strcat(combo, " / ");
    strcpy(combo, city);
    strcat(combo, ", ");
    strcpy(combo, st);
    strcat(combo, " ");
    strcpy(combo, zcode);
    strcat(combo, " / ");
    strcpy(combo, phone);
    
    /* Output */
    printf("\n%s\n", combo);
    
    return 0;
    }
    
    /* This routine fixes fgets incorporation of line feeds and other
     * ASCII values less than 32 into an input string.  Written by H. Brown.
     */
    int fixgets(char input_array[], int length, FILE *file_name)
       {
       register int position=0;
    
       if (fgets(input_array,length,file_name) != NULL)
         {
         while(input_array[position] != 0)
    	  {              if (input_array[position] < 32) input_array[position]=0;
    	  else ++position;
    	  }
         }
       /* The length of the string is returned. */
       fflush(stdin);
       return position;
       }
    The problem I have is that in the output section all it lists is the phone number, not "A12345 / Jones, James E / 234 S. Maple Street / San Fran, Ca 12345 / 345-123-4567". Not sure what I'm doing wrong. Any help is appreciated.
    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Cause that's what strcpy() does. It overwrites what's in there. Since you're strcpying the phone at the end, that's what the string will read.

    Try this:
    Code:
    strcpy(combo, cusnum);
    strcat(combo, " / ");
    strcat(combo, lname);
    strcat(combo, ", ");
    strcat(combo, fname);
    strcat(combo, " ");
    strcat(combo, mi);
    strcat(combo, " / ");
    strcat(combo, address);
    strcat(combo, " / ");
    strcat(combo, city);
    strcat(combo, ", ");
    strcat(combo, st);
    strcat(combo, " ");
    strcat(combo, zcode);
    strcat(combo, " / ");
    strcat(combo, phone);
    Sent from my iPadŽ

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Think of strcat looking something like:
    Code:
    char *strcat(char *dest, char *src)
    {
      return strcpy(dest + strlen(dest), src);
    }
    They're almost exactly the same. The only difference is that strcat() writes the src string at the end of dest and strcpy writes src at the beginning of it.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Maybe. willc0de4food (in the path) is another user. Maybe [s]he signed up twice.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    [S]he better hope that's the case, cause plagiarism is a bad bad offense.

    It is worth mentioning that both of these uses appear to be from Michigan according to thier profiles, so being the same person isn't out of the question.
    Last edited by SlyMaelstrom; 11-21-2005 at 05:32 PM.
    Sent from my iPadŽ

  7. #7
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    Heh, nope we're not the same people, but we do go to the same school. This is for a class and I'm assuming he either is taking, or has taken the class. I noticed the "fixgets" section was "written by H. Brown", which is my teacher. Looks like I may have to check up on his site next time I have something due ;-)

    And the fix worked, thank you very much. I knew the definition of strcat's and strcpy's, but I guess I wasn't really applying anything. Kinda obvious when I think of it. Feel kinda stupid, but I'm still thankful for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

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