Thread: printf is messing up and idk why

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    17

    printf is messing up and idk why

    These are my instructions:
    Define a struct to hold appropriate data elements. Create fields that are
    appropriate to the object you are dealing with. For example, if it is time,
    appropriate fields (in 12-hour format) might be hours, minutes, and ampm.
    Your program should use this template to create at least one instance of
    the structure, query the user for data to fill the fields, and output the contents.
    The purpose of this program is to insure that you can create and access a struct.

    I decided to do a program that will ask the user for their name, address, city, state, and zipcode. It's a simple program but whenever I try to execute it. This comes up


    Enter your full name:
    Enter your street address:

    They come up together and only lets me put in my street address, not letting me put in a name. I need help with fixing this.

    When all the information is printed out, it doesn't exactly line up with their titles either. I need help fixing this as well.

    This is my code:

    Code:
    
    
    Code:
    /* Program to print out address labels
    Written by KV
    December 2013
    Language: C (gcc format)
    */
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    #define NUM_LABEL 10
    #define FNAME 30
    #define STREET 30
    #define C_S_Z 30
    
    
    typedef struct
            {char name [FNAME];
             char address [STREET];
             char city_sz [C_S_Z];
             }address_label;
    
    
    int fill_info(address_label label[NUM_LABEL]);
    void print_label(address_label label[NUM_LABEL], int n);
    
    
     int main(void)
    { address_label label[NUM_LABEL];
      int n;
      n=fill_info(label);
      print_label(label,n);
    
    
      return 0;
    }
    
    
    int fill_info(address_label label[NUM_LABEL])
    /* Function to fill information on the labels
       Written by KV
       December 2013
       Language: C (gcc format)
    */
      {int i,n;
       printf("Enter the number of labels you want to make: ", n);
       scanf("%d", &n);
       for (i=0;i<n;i++)
       {printf("Enter your full name:\n ");
       gets(label[i] .name);
       printf("Enter your street address: ");
       gets(label[i] .address);
       printf("Enter your city, state and zipcode: ");
       gets(label[i] .city_sz);
       }
    return n;
    }
    
    
    void print_label(address_label label[NUM_LABEL], int n)
    /*Function to print out the address info
    Written by KV
    December 2013
    Language: C (gcc format)
    */
    
    
    {int i;
       printf("Name\t\tAddress\tCity State Zipcode\n");
       for (i=0; i<n; i++)
       {printf("%s \t", label[i].name);
        printf("%s \t", label[i].address);
        printf("%s \n", label[i].city_sz);
    }
    
    
    return;
    }



  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    I sorted of skimmed through your code but it sounds like to need to clear the input buffer after entering an the integer. When you enter an int and hit enter the newline character is skipping the string input and going to the next one. You need to clear the buffer after entering the int.

    I believe theres multiple ways of doing this, but I normally do this:
    Code:
    while(getchar() != '\n');
    Also, use fgets() instead of gets().

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    scanf leaves "\n" (the enter key or newline character), in the buffer after it reads it. Your next call to read the buffer, i.e. via gets, sees a newline chara and returns straight away. You need to read the newline char out of the buffer "by hand".

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
       printf("Enter the number of labels you want to make: ", n);
       scanf("%d", &n);
    The user enters a number and then enter.
    "scanf()" reads the number, but leaves the newline in the input buffer.
    (Side note: What is that 'n' doing in your "printf()"? You don't have any matching format specifiers - i.e., you're not printing any variables.)

    Code:
       {printf("Enter your full name:\n ");
       gets(label[i] .name);
    When you get to the first "gets()", it reads the newline that is still in the input buffer. This makes it seem as though that prompt is being skipped - but it's not.

    More information on that here: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    And read this, as well: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    When all the information is printed out, it doesn't exactly line up with their titles either. I need help fixing this as well.
    You're using tabs to line up the columns. But if a string is longer than a tab stop, the tab character brings it another tabstop forward. A good way around this is to use field width specifiers in your string outputs. Run the following example to see.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *string_1 = "short";
        char *string_2 = "longer_line";
        char *string_3 = "longest_line_ever";
    
        // your method
        // see how they don't line up nicely
        // if the strings are too long
        printf("Line 1\tLine 2\tLine 3\n");
        printf("%s\t",string_1);
        printf("%s\t",string_2);
        printf("%s\t",string_3);
        printf("\n\n");
    
        // another method
        printf("%-18s %-18s %-18s\n","Line 1","Line 2","Line 3");
        printf("%-18s ",string_1);
        printf("%-18s ",string_2);
        printf("%-18s ",string_3);
        printf("\n\n");
    
        return 0;
    }
    The number between the '%' and the 's' specifies the minimum width of the string. If shorter than this width, the string is padded with spaces. Also note how that number is negative. If positive, the strings default to right-justified (that is, the string is padded at the beginning). By making it negative, it is left-justified (the string is padded at the end).

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    I was able to clear the input buffer but I don't know how to use fgets. Especially with this code, I feel like I'll have to change it completely to use it.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    You shouldn't have to change it too much. The first one, for example:

    Code:
    fgets(label[i].name, FNAME, stdin);
    Also refer to fgets - C++ Reference

    Keep in mind, fgets() appends a newline at the end of the string, so you may need to replace it with a null terminator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. messing around with bitmaps
    By seanJ in forum C Programming
    Replies: 18
    Last Post: 01-04-2012, 04:52 PM
  2. CWinThread messing itself up
    By LowlyIntern in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 08:08 AM
  3. Messing with folders
    By Probose in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2006, 03:16 PM
  4. Is someone messing with m$ again?
    By exluddite in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-16-2004, 01:56 PM
  5. Do you think this might be messing up my brain?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-21-2002, 08:33 AM

Tags for this Thread