【发布时间】:2017-10-26 22:25:44
【问题描述】:
我正在写作业的一部分。
当我输入邮政编码(A8A 4J4)时,它会显示
应该是:
Postal code: A8A 4J4
City: Toronto
它正在跳过进入城市的选项。
我试过%[^\n] 仍然会跳过进入城市的选项。
我当前的代码是:
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%u", &address.apartmentNumber);
printf("Please enter the contact's postal code: ");
scanf("%s", &address.postalCode);
}
if (option == 'n' || option == 'N') {
printf("Please enter the contact's postal code: ");
scanf("%s", &address.postalCode);
}
printf("Please enter the contact's city: ");
scanf("%40s", address.city);
printf("Postal code: %s\n", address.postalCode);
printf("City: %s\n", address.city);
我已经看过一篇关于此的帖子,但那里的答案没有帮助。我已经在scanf 中尝试过 [^\n]。
【问题讨论】:
-
您可以尝试
scanf("%s ", &address.postalCode);吃掉尾随空格或scanf(" %40s", address.city);吃掉前导空格。
标签: c