[英]How do I take input from the user of their full name, and then output their first name and last name seperately? (C++)
[英]How do I take full name of students(including space) as input from console and store it in an array in c++?
如何从控制台输入学生的全名(包括空格)并将其存储在c ++中的数组中? 示例arr [0] = John Smith,arr [1] = David Clark? 从控制台输入John Smith和David Clark作为输入。
而不是常规的cin>>"John Smith";
,它不读取空格,因此您必须使用getline(cin, John Smith);
使用所需的大小初始化数组(数组的大小是输入的数量),并使用for()
循环数组的大小来打印它们(如果要打印它们)
while (getline(std::cin, sBuf))
vNames.push_back(sBuf);
其中sBuf
是std::string
和vNames
是std::vector<std::string>
这将为您提供所有想要的姓氏名称,直到剩下数据为止。
使用getline(cin, variableName);
。 cin
是您需要指定的输入流( cin
是控制台标准输入-键盘或文件变量),variableName是要存储在其中并带有适当索引号的数组的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.