2我的准则

时间:2018-01-11 06:50:42

标签: c

这是我的yes()代码的样子。我试图让它成为如果用户输入除了' y'之外的任何东西。 ' Y' ' N' ' N' 。我解决了输入除了那些字母之外的任何问题的问题,但是如果我要输入Yes或No,它只需要第一个字符,它使函数认为是其中一个字母。 我不知道如何解决这个问题。

        int yes(void) {
        char singleLetter = ' ';  
        int finalValue = -1; 
        int theResult = 0; 

        scanf(" %c", &singleLetter);
        clearKeyboard();

        do
        {
            switch (singleLetter)
            {

            case 'Y':
            case 'y':
                finalValue = 1;
                theResult = 1;
                break;

            case 'N':
            case 'n':
                finalValue = 0;
                theResult = 1;
                break;
            default:
                theResult = 0;
                printf("Only (Y)es or (N)o are acceptable: ");
                scanf("%c", &singleLetter);
                clearKeyboard();
            }
        } while (!theResult);

        return finalValue;

    }

我遇到的第二个问题是我得到了这个陈述:

  

此功能的目的是设置联系人使用的值   指针参数变量(设置它指向的联系人)。

     

使用接收到此功能的指针参数来提供   适当的联系成员到“get”函数(getName,   getAddress和getNumbers)设置联系人的值。

目前这是" get"部分:

void getName(struct Name *contactName) {

            printf("Please enter the contact's first name: ");
            scanf("%s", (*contactName).firstName);
            printf("Do you want to enter a middle intial(s)? (y or n): ");

            if (yes() == 1) {
                printf("Please enter the contact's middle intial(s): ");
                scanf("%s", (*contactName).middleInitial);

            }

            printf("Please enter the contact's last name: ");
            scanf("%s", (*contactName).lastName);
        }

        // getAddress:
        void getAddress(struct Address *

contactAddress) {


        printf("Please enter the contact's street number: ");

        (*contactAddress).streetNumber == getInt();

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

        printf("Do you want to enter an apartment number? (y or n): ");

        if (yes() == 1) {
            printf("Please enter the contact's apartment number: ");
            scanf("%d", (*contactAddress).apartmentNumber);
        }

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

    // getNumbers:


    // getNumbers:
    // NOTE:  Also modify this function so the cell number is
    //        mandatory (don't ask to enter the cell number)
    void getNumbers(struct Numbers *contactNumber) {

        printf("Please enter the contact's cell phone number: ");
        scanf(" %s", (*contactNumber).cell);
        printf("Do you want to enter a home phone number? (y or n) ");

        if (yes() == 1) {
            printf("Please enter the contact's home phone number: ");
            scanf("%s", (*contactNumber).home);
        }

        printf("Do you want to enter a business number? (y or n) ");

        if (yes() == 1) {
            printf("Please enter the contact's business phone number: ");
            scanf("%s", (*contactNumber).business);
        }
        printf("\n");
    }

然后我在下面有这个。而且我不确定该部分的编程方式。我试着把正确的东西(但显然是错的)

   void getContact(struct Contact *contact) {
        getName(contact);
        getAddress(contact);
        getNumbers(contact);
    }

这些是我教授所宣称的(所以我不能改变这一部分):

getContact(&contact);
    printf("\nValues Entered:\n");
    printf("Name: %s %s %s\n", contact.name.firstName, contact.name.middleInitial, contact.name.lastName);
    printf("Address: %d|%s|%d|%s|%s\n", contact.address.streetNumber, contact.address.street, 
        contact.address.apartmentNumber, contact.address.postalCode, contact.address.city);
    printf("Numbers: %s|%s|%s\n", contact.numbers.cell, contact.numbers.home, contact.numbers.business);

这是我头文件中的内容:

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

// Structure type Address declaration
// Place your code here...
struct Address {
    char street[41];
    int streetNumber[1];
    int apartmentNumber[1];
    char postalCode[8];
    char city[41];
};


// Structure type Numbers declaration
// Place your code here...
struct Numbers {
    char cell[21];
    char home[21];
    char business[21];
};


// Structure type Contact declaration
// Place your code here...
struct Contact {
    struct Name name;
    struct Address address;
    struct Numbers numbers;
};



//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------

// +-------------------------------------------------+
// | NOTE:  Copy/Paste your Assignment-2 Milestone-1 |
// |        function prototypes here...              |
// +-------------------------------------------------+


// Get and store from standard input the values for Name
// Place your code here...
void getName(struct Name *);

// Get and store from standard input the values for Address
// Place your code here...
void getAddress(struct Address *);

// Get and store from standard input the values for Numbers
// Place your code here...
void getNumbers(struct Numbers *);

// Get and store from standard input the values for a Contact
// Place your code here...
void getContact(struct Contact *);

使用以下说明运行程序时的结果

------------------------------------------
Testing: getContact(struct Contact *)
------------------------------------------
Please enter the contact's first name: Andrew
Do you want to enter a middle intial(s)? (y or n): n
Please enter the contact's last name: Random
Please enter the contact's street number: 100
Please enter the contact's street name: Rain
Do you want to enter an apartment number? (y or n): y
Please enter the contact's apartment number: 14
Please enter the contact's postal code: Z8Z 7Q7
Please enter the contact's city: Toronto
Please enter the contact's cell phone number: 647-999-9999
Do you want to enter a home phone number? (y or n) n
Do you want to enter a business number? (y or n) n


Values Entered:
Name: 647-999-9999  Random
Address: 13630948||13630952||
Numbers: ||

正如您所看到的,我的名字是我的手机号码,姓氏有效,地址根本不起作用,而且数字不存在。

1 个答案:

答案 0 :(得分:0)

我认为主要的问题是,在getContact()函数中,您将指针传递给Contact结构,而函数getName()等则需要指向内部结构(例如Name)的指针。将其更改为:

void getContact(struct Contact *contact) {
        getName(&contact->name);
        getAddress(&contact->address);
        getNumbers(&contact->numbers);
    }

请开始阅读警告信息!