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:
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.
streetNumberfield ofAddressan array of oneint? Why not just anint? Anyway, no need for the&inscanf("%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 toscanf()always.