accepting long strings with spaces

This is a discussion on accepting long strings with spaces within the C Programming forums, part of the General Programming Boards category; I am using the Visual C++ compiler, and running on WIN2000. I am trying to accept input fields like address, ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    12

    accepting long strings with spaces

    I am using the Visual C++ compiler, and running on WIN2000.

    I am trying to accept input fields like address, city, and state.

    States are usually 2 letter initials, but address and cities may invlove more than 2 characters and may have a space between each word.

    How do I accept input from the user when the input involves long strings with spaces between each word? I can't seem to allow input from address and city both at the same time.

    I found a way to accept the address, but when I use the same stratagy for the city, it doesn't work. Please help!

    Check it out:
    Code:
    char strPatientAddress[BUFSIZ];
    char strPatientCity[BUFSIZ];
    char strPatientState[50];
    char *p;
    char *a;
    
    char *fgets(char *s, int size, FILE *stream);
    
    
    printf(" Please enter in data for the following:\n\n");
    
    printf(" Address: ");	
    	
    fgets( strPatientAddress, sizeof(strPatientAddress), stdin);
    
    		
    //checks to see if variable contains a value
    if (fgets(strPatientAddress, sizeof(strPatientAddress), stdin) != NULL)
    {
        //test for and remove newline character
        if ((p = strchr(strPatientAddress, '\n')) != NULL)
        *p = '\0';
    }
    printf("\n");
      	
    printf(" City: ");	
    	
    fgets( strPatientCity, sizeof(strPatientCity), stdin);
    
    		
    //checks to see if variable contains a value
    if (fgets(strPatientCity, sizeof(strPatientCity), stdin) != NULL)
    {
        //test for and remove newline character
        if ((a = strchr(strPatientCity, '\n')) != NULL)
        *a = '\0';
    }
    printf("\n");
    
    		
    printf(" State Initials: ");
    scanf(" %s", strPatientState);
    printf("\n");
    Mod edit: Please review your posts to make sure your tags are correct
    Regards,
    Trang

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Yeah, you've made the fatal mistake of throwing in a scanf() call whilst using fgets()

    Use fgets() for everything!
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Proper way to close a code tag is with [/code]
    2) // is C++ unless you have a C99 compiler. /* */ is the correct comment usage in C
    3)
    Code:
    fgets( strPatientAddress, sizeof(strPatientAddress), stdin);
    
    //checks to see if variable contains a value
    if (fgets(strPatientAddress, sizeof(strPatientAddress), stdin) != NULL)
    Is asking the user to input the data TWICE. Remove the call before the if statement here and in the call for the City and it should work.
    4) Don't mix scanf and fgets. Use fgets to get the state also.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
      char strPatientAddress[BUFSIZ];
      char strPatientCity[BUFSIZ];
      char strPatientState[50];
      char *p;
      char *a;
    
      printf(" Please enter in data for the following:\n\n");
      printf(" Address: "); 
    
      /*checks to see if variable contains a value*/
      if (fgets(strPatientAddress, sizeof(strPatientAddress), stdin) != NULL)
      {
        /*test for and remove newline character*/
        if ((p = strchr(strPatientAddress, '\n')) != NULL)
          *p = '\0';
      }
      
      printf("\n City: "); 
    
      /*checks to see if variable contains a value*/
      if (fgets(strPatientCity, sizeof(strPatientCity), stdin) != NULL)
      {
        /*test for and remove newline character*/
        if ((a = strchr(strPatientCity, '\n')) != NULL)
          *a = '\0';
      }
      
      printf("\n State Initials: ");
      
      scanf(" %s", strPatientState);
      printf("\n");
      /* My Addition to make sure correct info was recieved */
      printf("%s\n%s, %s\n", strPatientAddress, strPatientCity, strPatientState);
    }
    Last edited by Thantos; 01-05-2004 at 03:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  2. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  3. Concatnating strings that are too long for one line
    By 691175002 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2007, 11:24 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. strings with spaces
    By Wanted420 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2003, 10:33 PM

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