Попытка запустить этот код покажет пустые входные данные для большинства полей и будет перепутана, например, номер улицы будет стоять вместо имени. Не уверен, что происходит. Сначала колебался с использованием stackoverflow, но теперь кажется, что у меня нет выбора
заголовочный файл:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
struct Address {
int streetNumber;
char street[41];
int apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
struct Numbers {
char cell[11];
char home[11];
char business[11];
};
// Structure type Contact declaration
struct Contact {
struct Name name;
struct Address address;
struct Numbers numbers;
};
//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------
// Get and store from standard input the values for Name
void getName(struct Name* name);
// Get and store from standard input the values for Address
void getAddress(struct Address* address);
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers);
входной файл
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
//This function will clear the input buffer after every input
void clear() {
while (getchar() != '\n');
}
// Get and store from standard input the values for Name
void getName(struct Name* name) {
char option = 0;
printf("Please enter the contact's first name: ");
scanf("%31s", name[0].firstName);
clear();
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%7[^\n]s", name[0].middleInitial);
clear();
}
printf("Please enter the contact's last name: ");
scanf("%36[^\n]s", name[0].lastName);
clear();
}
// Get and store from standard input the values for Address
void getAddress(struct Address* address) {
char option = 0;
printf("Please enter the contact's street number: ");
scanf("%d", &address[0].streetNumber);
clear();
printf("Please enter the contact's street name: ");
scanf("%40[^\n]s", address[0].street);
clear();
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%d", &address[0].apartmentNumber);
clear();
}
printf("Please enter the contact's postal code: ");
scanf("%7[^\n]s", address[0].postalCode);
clear();
printf("Please enter the contact's city: ");
scanf("%40[^\n]s", address[0].city);
clear();
}
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers) {
char option = 0;
printf("Do you want to enter a cell phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf("%11s", numbers[0].cell);
clear();
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf("%11s", numbers[0].home);
clear();
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf("%11s", numbers[0].business);
clear();
}
}
моя основная программа:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables:
struct Contact contact[] = {{0}};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(contact);
// Call the Contact function getAddress to store the values for the Address member
getAddress(contact);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(contact);
// Display Contact summary details
printf("\n");
printf("Contact Details\n");
printf("===============\n");
printf("Name Details\n");
printf("------------\n");
printf("First name: %s\n", contact[0].name.firstName);
printf("Middle initial(s): %s\n", contact[0].name.middleInitial);
printf("Last name: %s\n", contact[0].name.lastName);
printf("\n");
printf("Address Details\n");
printf("--------------\n");
printf("Street number: %d\n", contact[0].address.streetNumber);
printf("Street name: %s\n", contact[0].address.street);
printf("Apartment: %d\n", contact[0].address.apartmentNumber);
printf("Postal code: %s\n", contact[0].address.postalCode);
printf("City: %s\n", contact[0].address.city);
printf("\n");
printf("Phone Numbers\n");
printf("-------------\n");
printf("Cell phone number: %s\n", contact[0].numbers.cell);
printf("Home phone number: %s\n", contact[0].numbers.home);
printf("Business phone number: %s\n", contact[0].numbers.business);
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
любая помощь будет принята с благодарностью, спасибо