为什么第一个printf没有';当另外两个结构相同时,它不工作吗? \define\u CRT\u SECURE\u NO\u警告 #包括 内部主(空) { 结构数 { 炭细胞[21]; char home[21]; 煤焦业务[21]; }; //在此处声明变量: 结构数; 字符提示; printf(“您想输入手机号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的手机号码:”); scanf(“%s”,number.cell); } printf(“您想输入家庭电话号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的家庭电话号码:”); scanf(“%s”,number.home); } printf(“您想输入业务电话号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的业务电话号码:”); scanf(“%s”,数字。业务); } printf(“\n”); printf(“电话号码:”); printf(“\n”); printf(“手机号码:%s”,number.Cell); printf(“\n”); printf(“家庭电话号码:%s”,number.Home); printf(“\n”); printf(“业务电话号码:%s”,number.Business); printf(“\n”); printf(“完成名称、地址和数字的结构测试!”); 返回0; }
以下是输出的外观: 当家庭电话和商务电话工作时,手机号码为空。 有一次我输入了一个错误,y->12345->y->12345->y12345->12345,然后所有3个数字都正确显示。我找不到有什么问题 输入错误的结果:为什么第一个printf没有';当另外两个结构相同时,它不工作吗? \define\u CRT\u SECURE\u NO\u警告 #包括 内部主(空) { 结构数 { 炭细胞[21]; char home[21]; 煤焦业务[21]; }; //在此处声明变量: 结构数; 字符提示; printf(“您想输入手机号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的手机号码:”); scanf(“%s”,number.cell); } printf(“您想输入家庭电话号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的家庭电话号码:”); scanf(“%s”,number.home); } printf(“您想输入业务电话号码吗?(y或n):”; scanf(“%s”,提示(&P); 如果(提示='y'| |提示=='y') { printf(“请输入联系人的业务电话号码:”); scanf(“%s”,数字。业务); } printf(“\n”); printf(“电话号码:”); printf(“\n”); printf(“手机号码:%s”,number.Cell); printf(“\n”); printf(“家庭电话号码:%s”,number.Home); printf(“\n”); printf(“业务电话号码:%s”,number.Business); printf(“\n”); printf(“完成名称、地址和数字的结构测试!”); 返回0; },c,C,以下是输出的外观: 当家庭电话和商务电话工作时,手机号码为空。 有一次我输入了一个错误,y->12345->y->12345->y12345->12345,然后所有3个数字都正确显示。我找不到有什么问题 输入错误的结果: 打印输出的问题来自覆盖结构的第一个成员 字节序列{'y','\0'}正在写入单字节提示符以及随后的内容,这几乎肯定是数字的第一个字节。因此,这将单元格字段的第一个字符设置为'\0',使其成为空字符串 如果要在scanf中使用字符串格式“%s”(“%s”,&prompt)提示必
打印输出的问题来自覆盖结构的第一个成员 字节序列
{'y','\0'}
正在写入单字节提示符
以及随后的内容,这几乎肯定是数字
的第一个字节。因此,这将单元格
字段的第一个字符设置为'\0'
,使其成为空字符串
如果要在scanf中使用字符串格式“%s”
(“%s”,&prompt)代码>提示必须为字符串类型。Char变量无法保存您的scanf
读取的字符加空字符串终止符
更自然和典型的方法是使用“%c”
读取字符。两种方法都有介绍
注1:
%c
转换说明符不会自动跳过任何前导空格,因此,如果输入流中有一个错误的换行符(例如,来自上一个条目),scanf调用将立即使用它
解决此问题的一种方法是在格式字符串中的转换说明符之前放置一个空格:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main (void)
{
struct Numbers
{
char cell[21];
char home[21];
char business[21];
};
// Declare variables here:
struct Numbers numbers;
char prompt;
printf("Do you want to enter a cell phone number? (y or n): ");
scanf("%s", &prompt);
if (prompt == 'y' || prompt == 'Y')
{
printf("Please enter the contact's cell phone number: ");
scanf("%s", numbers.cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%s", &prompt);
if (prompt == 'y' || prompt == 'Y')
{
printf("Please enter the contact's home phone number: ");
scanf("%s", numbers.home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%s", &prompt);
if (prompt == 'y' || prompt == 'Y')
{
printf("Please enter the contact's business phone number: ");
scanf("%s", numbers.business);
}
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", numbers.cell);
printf("\n");
printf("Home phone number: %s", numbers.home);
printf("\n");
printf("Business phone number: %s", numbers.business);
printf("\n");
printf("Structure test for Name, Address, and Numbers Done!");
return 0;
}
注2:通常我们在main
之前声明结构,以便在文件中为它们提供全局范围
注3:另外,如果将numbers
声明为本地结构变量,则所有结构成员都未定义。如果用户跳过填写数字,您可能会得到该字段的垃圾打印输出。您有义务自己初始化这些成员
scanf(" %c", &c);
如果您可以复制并通过文本scanf(“%s”,&prompt),请不要发布图像代码>->这是错误的-它将把空字符放在哪里来标记字符串的结尾?因此,逻辑问题是-如果他们不想在电话号码中输入“空”字符,您是否填写了“空”字符?为什么您在将%s
提示设置为字符时使用提示而不是字符提示[10]代码>和scanf(“%s”,提示(&P)
,我们可以做字符提示
和scanf(“%c”,&prompt)
@H.S.谢谢您的评论!我们当然可以!我只是想重用%s
。不过你的建议更自然。谢谢也许值得一提的是为什么会发生这样的事情。换句话说,字节序列{'y','\0'}
正在写入单字节提示符以及随后的内容,这几乎肯定是数字的第一个字节。这将单元格
字段的第一个字符设置为\0
,使其成为空字符串。@paxdiablo谢谢!这是一个很好的评论!我会把它包括在答案中。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct Numbers
{
char cell[21];
char home[21];
char business[21];
};
int main (void)
{
// Declare variables here:
char prompt[10];
char prompt1;
struct Numbers numbers;
numbers.cell[0] = '\0';
numbers.home[0] = '\0';
numbers.business[0] = '\0';
printf("Do you want to enter a cell phone number? (y or n):\n");
scanf(" %c", &prompt1);
if (prompt1 == 'y' || prompt1 == 'Y')
{
printf("Please enter the contact's cell phone number:\n");
scanf("%s", numbers.cell);
}
printf("Do you want to enter a home phone number? (y or n):\n");
scanf(" %c", &prompt1);
if (prompt1 == 'y' || prompt1 == 'Y')
{
printf("Please enter the contact's home phone number:\n");
scanf("%s", numbers.home);
}
printf("Do you want to enter a business phone number? (y or n):\n");
scanf("%s", &prompt);
if (*prompt == 'y' || *prompt == 'Y')
{
printf("Please enter the contact's business phone number:\n");
scanf("%s", numbers.business);
}
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", numbers.cell);
printf("\n");
printf("Home phone number: %s", numbers.home);
printf("\n");
printf("Business phone number: %s", numbers.business);
printf("\n");
printf("Structure test for Name, Address, and Numbers Done!");
return 0;
}
Do you want to enter a cell phone number? (y or n):
y
Please enter the contact's cell phone number:
123
Do you want to enter a home phone number? (y or n):
y
Please enter the contact's home phone number:
456
Do you want to enter a business phone number? (y or n):
y
Please enter the contact's business phone number:
789
Phone Numbers:
Cell phone number: 123
Home phone number: 456
Business phone number: 789
Structure test for Name, Address, and Numbers Done!