C 我的代码有两个主要问题
这就是我的yes代码的样子。我正在尝试这样做,如果用户输入的不是“y”“y”“n”“n”。我解决了输入除这些字母以外的任何字符的问题,但是如果我输入Yes或No,它只需要第一个字符,这使函数认为它是这些字母中的一个。 我不知道如何解决这个问题C 我的代码有两个主要问题,c,C,这就是我的yes代码的样子。我正在尝试这样做,如果用户输入的不是“y”“y”“n”“n”。我解决了输入除这些字母以外的任何字符的问题,但是如果我输入Yes或No,它只需要第一个字符,这使函数认为它是这些字母中的一个。 我不知道如何解决这个问题 int yes(void) { char singleLetter = ' '; int finalValue = -1; int theResult = 0; sc
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和GetNumber设置联系人的值
这是当前“获取”部分的内容:
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等函数需要指向内部结构(如name)的指针。将其更改为:
void getContact(struct Contact *contact) {
getName(&contact->name);
getAddress(&contact->address);
getNumbers(&contact->numbers);
}
请开始阅读警告信息 我只读了第一段,但是如果您想将输入处理为Yes或No,char不是正确的数据类型。你需要一个字符串,这是C还是C++还是ObjuleC?请选择oneRecommend删除大多数语言标记。看起来更像C,而不是C++或ObjuleC,所以如果你和C.@ DEIDI一起改变,你可能会有更少的重写,我已经把它改为Char SigLeLeTter(100);int finalValue=-1;int-theResult=0;扫描%s,单个字母(&s)[0];清晰键盘;do{switch singleLetter[0]但其结果相同switch singleLetter[0]仍然只检查第一个字符。switch不是作业的工具。我已经这样做了。但我最终在main中得到了对“getContact”的错误未定义引用,该引用具有struct Contact={{{0}};getContact&contact;printf\n输入的值:\n;printfName:%s%s\n,contact.name.firstName,contact.name.middleInitial,contact.name.lastName;PrintFadAddress:%d |%s |%d |%s\n,contact.address.street,contact.address.street,contact.address.apartmentNumber,contact.address.postalCode,contact.address.city;PrintFNumber:%s|%s |%s\n,contact.numbers.cell,contact.numbers.home,contact.numbers.business;