简体   繁体   English

如何从控制台输入学生的全名(包括空格)并将其存储在c ++中的数组中?

[英]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> 其中sBufstd::stringvNamesstd::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.

相关问题 我如何从他们的全名的用户那里得到输入,然后 output 他们的名字和姓氏分开? (C++) - How do I take input from the user of their full name, and then output their first name and last name seperately? (C++) 如何从 C++ 文件中读取由空格分隔的全名? - How do I read a full name that is separated by a space from a file in C++? 如何接受用户输入,对该输入进行数学运算,并将结果存储在数组中? 在 C++ - how to take user input, do math to that input, and store the results in an array? in C++ 如何获取数组中以空格分隔的整数的输入(C ++) - How to take input of space seperated integers in an array (c++) 如何在 C++ 中均匀间隔数组中的数字? - How do I evenly space numbers from an array in C++? 如何从用户(这是一个列表)获取输入并将值存储在 C++ 中的数组中? - How to take input from user ( which is a list ) and store values in array in c++? 如何在 C++ 中输入并将其“拆分”为列表? 换句话说,取 N 个输入并放入一个长度为 N 的数组中 - How do I take an input in C++ and “split” it into a list? In other words, take N inputs and put in into an array of N length 如何从数组中获取输入并将其输入到C ++中的方程式中? - How to take inputs from array and input it into an equation in C++? 如何将用户输入作为密码并将其与 c++ 文件中的数据进行比较 - How do I take input from user as a password and compare it with the data in the file in c++ 如何在C ++控制台应用程序中将输入与输出分开? 我可以有两个光标吗? - How do I separate input from output in a C++ console application? Can I have two cursors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM