如何允许无限数量的名称输入
How to allow unlimited number of names for input?
#include <iostream>
using namespace std;
#include "ReadString.h"
void main()
{
int i ;
int NumNames=5;
char ** pNames;
int MaxNum = 10;
int more(0);
pNames = new char *[NumNames];
cout << "Enter names" << endl;
这是我遇到麻烦的部分。我以不同的方式尝试,但没有成功。我试图做一个循环,除非第一个字符是"回车键"。
while(cin.get()!='n')
{
for (i = more; i < NumNames; i++)
{
cout << (i + 1) << ") ";
pNames[i] = ReadString();
more = NumNames;
NumNames +=NumNames
}
}
将手动分配的动态数组替换为一些可以动态增长的容器,例如std::vector
:
std::vector<std::string> names;
std::string name;
// read the names
while (cin >> name) {
names.push_back(name);
}
相关文章:
- 如何只允许用户输入正整数
- 如何允许大写和小写用户输入?
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 创建一个数组,以允许用户输入存储并稍后在代码中使用
- 如何允许用户在对象和类中输入?
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 该程序允许用户处理循环,只要用户输入奇数
- 允许用户输入带或不带美元符号的金额
- 不允许在C++中输入相同的输入
- 如何从 c++ 控制台读取未知行数(允许空行)上的输入
- 输入名称bool不允许.C
- 如何允许用户在进行错误选择时再次输入
- 2D 数组如何允许输入中的空格
- 功能调用不起作用,但控制台仍允许输入
- CIN 在首次使用功能后不允许输入
- C 仅允许一个输入
- 如何允许无限数量的名称输入
- cin.clear() 不允许我进行新输入
- boost::p osix_time::from_iso_string 允许错误的输入
- cin 的 Xcode 问题(在 Xcode7.3 中会自动给出零且不允许用户输入)