Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i dont know how make this right

+1 vote
asked 6 days ago by NUR AMIR ABQARI BIN JAFRI BIN JAFRI (140 points)
#include <stdio.h>

int main()
{
    char name[30], blank;
    char add[90];
    char id;
    char state;
    int code;
    
    printf("Enter your name:");
    scanf("%s %[^\n]", &name, &blank);
    printf("Enter your ID number:");
    scanf("%s", &id);
    printf("Enter your address:");
    scanf("%s %[^\n]", &add, &blank);
    printf("State:");
    scanf("%s", &state);
    printf("postal code:");
    scanf("%d", &code);
    
    printf("Name\t\tID Number\t\tAddress");
    printf("\n====\t\t==========\t\t=======");
    printf("\n %s \t\t %s \t\t %s %d %s", name, id, add, code, state);
    
    
    

    return 0;
}

1 Answer

0 votes
answered 5 days ago by Peter Minarik (34,450 points)
edited 5 days ago by Peter Minarik

I believe what you're trying to do is reading a string that contains spaces (e.g. in the name or in the address). scanf() stops reading from the standard input when it hits a whitespace or end-of-line character. So it is not suited for your needs.

You should use fgets() instead. You can set the target buffer, the maximum number of bytes to read and where to read from (stdin). This introduces a new problem though: fgets() also returns the newline character entered, so one has to manually remove it if it is unwanted. For this, I created a function called GetString().

I also took the liberty to make your output formatting better. Please, read carefully how printf() works. You can specify the width of the output and even you can do so that this width is not fixed in the format string, but comes as an additional argument before the variable to be output (specified via the * in the format string). Also, you can specify whether this should be left justifier (-) or right justified (default behaviour).

Here's the code for you

#include <stdio.h>

#define NAME_LENGTH         30
#define ADDRESS_LENGTH      50
#define ID_LENGTH           20
#define STATE_LENGTH        30
#define POSTAL_CODE_LENGTH   8

void GetString(const char * prompt, char * buffer, size_t size)
{
    printf("%s", prompt);
    fgets(buffer, size, stdin);
    // Removing the newline character
    for (char * ch = buffer; *ch != '\0'; ch++)
    {
        if (*ch == '\n')
        {
            *ch = '\0';
            break;
        }
    }
}

int main()
{
    char name[NAME_LENGTH];
    char id[ID_LENGTH];
    char add[ADDRESS_LENGTH];
    char state[STATE_LENGTH];
    int postalCode;

    // Getting input from user
    GetString("Enter your name: ", name, sizeof(name));
    GetString("Enter your ID number: ", id, sizeof(id));
    GetString("Enter your address: ", add, sizeof(add));
    GetString("Enter your state: ", state, sizeof(state));
    printf("Postal code: ");
    scanf("%d", &postalCode);
    
    // Printing the header
    printf("%-*s%-*s%-*s\n", NAME_LENGTH, "Name", ID_LENGTH, "ID", ADDRESS_LENGTH + POSTAL_CODE_LENGTH + STATE_LENGTH, "Address");
    for (int i = 0; i < NAME_LENGTH + ID_LENGTH + ADDRESS_LENGTH + POSTAL_CODE_LENGTH + STATE_LENGTH; i++)
        printf("=");
    printf("\n");

    // Printing the data
    printf("%-*s%-*s%s %d %s\n", NAME_LENGTH, name, ID_LENGTH, id, add, postalCode, state);

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...