【发布时间】:2019-10-24 15:35:08
【问题描述】:
所以我想在同一个scanf_s 中写两个变量。也许我什至没有使用正确的名称来描述我想要的东西,因为我是新手,但基本上我希望它是这样的:
What is your last and first name: John Smith
Thank you now I know that your first name is John and your last name is Smith
我写的是这样的:
#include <iostream>
int main(void)
{
char myFirstName[20];
char myLastName[20];
printf("\nWhat is your first and last name: ");
scanf_s("???")
printf("Thank you now I know that your first name is %s", myFirstName); printf(" and your last name
is %s\n",myLastName);
getchar();
return 0;
}
而且我不知道应该如何编写 scanf_s 部分来包含两个变量(myFirstName 和 myLastName),所以它会按照我的意愿出现。
【问题讨论】:
-
你能用
scanf_s读取一个字符串吗?对于scanf(以及scanf_s等相关函数)或printf,格式说明符的数量没有真正的限制。 -
没有 C 头
iostream。你用 C 或 C++ 编译器编译吗?这些是不同的语言。 -
Don't use
scanfto read interactive input;它无法处理用户输入的内容与您的预期不同。使用fgets读取用户键入的整行内容,然后使用strsep等函数对其进行解析。 Don't usescanf_s, or any of the other_sfunctions, at all;它们不可移植,也不能解决它们想要解决的问题。