指针和/或功能无法正常工作

尝试运行此代码将为大多数字段显示空白输入,并且会混淆,例如,街道号将代替名字。不知道发生了什么。起初对使用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[] = 

指针和/或功能无法正常工作

; // 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; }

任何帮助将不胜感激,谢谢

jianglongdi 回答:指针和/或功能无法正常工作

调用函数时main出现错误

getName(contact);
getAddress(contact);
getNumbers(contact);

当他们等待其他结构类型时,您传递了一个接触结构。

void getName(struct Name* name);
void getAddress(struct Address* address);
void getNumbers(struct Numbers* numbers);

尝试使用

更正此问题
getName(&contact->name);
getAddress(&contact->address);
getNumbers(&contact->numbers);
本文链接:https://www.f2er.com/3120526.html

大家都在问