2

I'm trying to complete my assignment and I'm almost done, except these 2 problems that I couldn't find the source of. So my code is basically a simple contact management system that stores the information in structs. These are my structs:

struct Name {
    char firstName[31];
    char middleInitial[7];
    char lastName[36];
};

struct Address {
    char street[40];
    char postalCode[7];
    char city[40];
    int streetNumber[1];
    int apartmentNumber[31];
};

struct Numbers {
    char cell[20];
    char home[20];
    char business[20];
};

And my program gathers information from the user by asking questions like "Please type the contacts name:", "Please type the contacts number:" and so on. This is my code:

#include <stdio.h>
#include "contacts.h"

int main(void)

{
    // Declare variables here:
    struct Name contacts;
    struct Address address;
    struct Numbers number;
    char select1, select2, select3, select4, select5;
    // Display the title

    printf("Contact Management System\n-------------------------\n");

    // Contact Name Input:

    printf("Please enter the contact's first name: ");
    scanf(" %[^\n]", contacts.firstName);
    printf("Do you want to enter a middle initial(s)? (y or n): ");
    scanf(" %c", &select1);

    if (select1 == 'y' || select1 == 'Y') {
        printf("Please enter the contact's middle initial(s): ");
        scanf(" %[^\n]", contacts.middleInitial);
    }

    printf("Please enter the contact's last name: ");
    scanf(" %[^\n]", contacts.lastName);

    // Contact Address Input:
    printf("Please enter the contact's street number: ");
    scanf("%d", &address.streetNumber);

    printf("Please enter the contact's street name: ");
    scanf(" %[^\n]", address.street);

    printf("Do you want to enter an appartment number? (y or n): ");
    scanf(" %c", &select2);

    if (select2 == 'y' || select2 == 'Y') {
        printf("Please enter the contact's appartment number: ");
        scanf(" %d", &address.apartmentNumber);
    }

    printf("Please enter the contact's postal code: ");
    scanf(" %[^\n]", address.postalCode);

    printf("Please enter the contact's city: ");
    scanf(" %[^\n]", address.city);

    // Contact Numbers Input:
    printf("Do you want to enter a cell phone number? (y or n): ");
    scanf(" %c", &select3);

    if (select3 == 'y' || select3 == 'Y') {
        printf("Please enter the contact's cell phone number: ");
        scanf(" %[^\n]", number.cell);
    }

    printf("Do you want to enter a home phone number? (y or n): ");
    scanf(" %c", &select4);

    if (select4 == 'y' || select4 == 'Y') {
        printf("Please enter the contact's home phone number: ");
        scanf(" %[^\n]", number.home);
    }

    printf("Do you want to enter a business phone number? (y or n): ");
    scanf(" %c", &select5);

    if (select5 == 'y' || select5 == 'Y') {
        printf("Please enter the contact's business phone number: ");
        scanf(" %[^\n]", number.business);
    }

    // Display Contact Summary Details

    printf("\nContact Details\n---------------");

    printf("\nName Details");

    printf("\nFirst name: %s", contacts.firstName);

    if (select1 == 'y' || select1 == 'Y') {
        printf("\nMiddle initial(s): %s", contacts.middleInitial);
    }
    printf("\nLast name: %s", contacts.lastName);

    printf("\n");

    printf("\nAddress Details");

    printf("\nStreet number: %d", address.streetNumber);

    printf("\nStreet name: %s", address.street);

    if (select2 == 'y' || select2 == 'Y') {
        printf("\nApartment: %d", address.apartmentNumber);
    }
    printf("\nPostal code: %s", address.postalCode);

    printf("\nCity: %s", address.city);

    printf("\n");

    printf("\nPhone Numbers:");

    if (select3 == 'y' || select3 == 'Y') {
        printf("\nCell phone number: %s", number.cell);
    }
    if (select4 == 'y' || select4 == 'Y') {
        printf("\nHome phone number: %s", number.home);
    }
    if (select5 == 'y' || select5 == 'Y') {
        printf("\nBusiness phone number: %s", number.business);
    }

    // Display Completion Message

    printf("\n");

    printf("\nStructure test for Name, Address, and Numbers Done!\n");

    return 0;
}

When I enter the following sample data:enter image description here

This is my output:

Contact Details                                                                                                                         
---------------                                                                                                                         
Name Details                                                                                                                            
First name: Tom                                                                                                                         
Middle initial(s): Wong                                                                                                                 
Last name: Song                                                                                                                         

Address Details                                                                                                                         
Street number: -1206060696                                                                                                              
Street name: Sunrise                                                                                                                    
Apartment: -1206060692                                                                                                                  
Postal code: M4A 2S2Toronto                                                                                                             
City: Toronto                                                                                                                           

Phone Numbers:                                                                                                                          
Cell phone number: 647-898-1453                                                                                                         
Home phone number: 226-218-0410                                                                                                         
Business phone number: 416-888-2222                                                                                                     

Structure test for Name, Address, and Numbers Done!

As you can see, my street number and apartment number are totally something else and the input for the city concatenates with the input for the postal code. I tried to see if I missed an \n somewhere but I haven't.

1
  • 1
    Why is the streetNumber field of Address an array of one int? Why not just an int? Anyway, no need for the & in scanf("%d", &address.streetNumber);, since arrays decay to pointers to their first elements in most expressions. Also, note that you should check the value returned from calls to scanf() always. Commented Nov 30, 2017 at 2:50

3 Answers 3

4

When I compile your code using GCC 7.2.0 (on a Mac running macOS High Sierra 10.13.1), I get the error reports:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes ad79.c -o ad79
ad79.c: In function ‘main’:
ad79.c:55:13: error: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[1]’ [-Werror=format=]
     scanf("%d", &address.streetNumber);
            ~^   ~~~~~~~~~~~~~~~~~~~~~
ad79.c:65:18: error: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[31]’ [-Werror=format=]
         scanf(" %d", &address.apartmentNumber);
                 ~^   ~~~~~~~~~~~~~~~~~~~~~~~~
ad79.c:116:31: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Werror=format=]
     printf("\nStreet number: %d", address.streetNumber);
                              ~^   ~~~~~~~
                              %ls
ad79.c:121:31: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Werror=format=]
         printf("\nApartment: %d", address.apartmentNumber);
                              ~^   ~~~~~~~
                              %ls
cc1: all warnings being treated as errors
$

You shouldn't be passing pointers to arrays to scanf(), and you shouldn't be passing pointers to printf().

If your compiler doesn't have analogously helpful error reporting options, it might be time to switch compilers.

Simplest fix:

In struct Address change:

int streetNumber[1];
int apartmentNumber[31];

to:

int streetNumber;
int apartmentNumber;

(A single apartment usually only has one number — even if a block of apartments might have more, but would it necessarily only have 31?)

This has only fixed the most egregious problems. There could be (and probably are) other problems left. They are, however, more subtle.

Sign up to request clarification or add additional context in comments.

Comments

2

Notice your

struct Address {
    ...
    int streetNumber[1];
    ...
};

The streetNumber 's type is array, not int.
Use printf("%d", address.streetNumber[0]) and scanf("%d", &address.streetNumber[0]) print or input.
Or just change your struct to

struct Address {
    ...
    int streetNumber;
    ...
};

1 Comment

This is one of the problems, for sure.
2

The other issue not yet commented on: the array for the postal code is too short. You write a string into it, which has a terminating null character. This one doesn't fit, so when printing the string the next one is catenated.

2 Comments

So what's your solution?
Make the array for the postal code one element longer. But really, you need to check your input and make sure you don't overflow your buffers. What's to stop someone from entering a postal code with 8 characters? Or 20? Validating input is a must!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.