[英]How do I take full name of students(including space) as input from console and store it in an array in c++?
How do I take full name of students(including whitespace) as input from console and store it in an array in c++? 如何从控制台输入学生的全名(包括空格)并将其存储在c ++中的数组中? Example arr[0]=John Smith, arr[1]=David Clark?
示例arr [0] = John Smith,arr [1] = David Clark? where John Smith and David Clark is entered from console as input.
从控制台输入John Smith和David Clark作为输入。
Instead of the regular cin>>"John Smith";
而不是常规的
cin>>"John Smith";
, which doesnt read spaces, u have to use getline(cin, John Smith);
,它不读取空格,因此您必须使用
getline(cin, John Smith);
Initialize the array with the size you want ( the size of it are the number of inputs) and use a for()
loop the size of the array to print them (in case you want to print them) 使用所需的大小初始化数组(数组的大小是输入的数量),并使用
for()
循环数组的大小来打印它们(如果要打印它们)
while (getline(std::cin, sBuf))
vNames.push_back(sBuf);
where sBuf
is std::string
and vNames
is std::vector<std::string>
其中
sBuf
是std::string
和vNames
是std::vector<std::string>
This will get you all the names with surnames you want till there is data left. 这将为您提供所有想要的姓氏名称,直到剩下数据为止。
Use getline(cin, variableName);
使用
getline(cin, variableName);
. 。 Where as
cin
is the input stream you need to specify (it could by cin
which is console standard input - keyboard or file variable), a variableName is the name of an array you want to store it in with appropriate index number. cin
是您需要指定的输入流( cin
是控制台标准输入-键盘或文件变量),variableName是要存储在其中并带有适当索引号的数组的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.