gpt4 book ai didi

c - 2 我的代码的主要问题

转载 作者:行者123 更新时间:2023-11-30 21:27:43 25 4
gpt4 key购买 nike

这就是我的 yes() 代码的样子。我试图做到这一点,以便如果用户输入 'y' 'Y' 'n' 'N' 以外的任何内容。我解决了输入除这些字母以外的任何内容的问题,但如果我输入"is"或“否”,它只需要第一个字符,这使得函数认为它是这些字母之一。我不知道如何解决这个问题。

        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;

}

我遇到的第二个问题是我得到了这样的声明:

The purpose of this function is to set the values for a Contact using the pointer parameter variable (set the Contact it points to).

Use the pointer parameter received to this function to supply the appropriate Contact member to the “get” functions (getName, getAddress, and getNumbers) to set the values for the Contact.

这是当前“获取”部分的内容:

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: ||

如您所见,我的名字是我的手机号码,姓氏有效,地址根本无效,而且号码也不存在。

最佳答案

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

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

请开始阅读警告消息!

关于c - 2 我的代码的主要问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48201297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com