【发布时间】:2014-04-06 18:33:12
【问题描述】:
这是我的代码:
...
...
do
{
cin >> command;
switch(command)
{
case 'i':
cin >> key >> nome >> idade >> endereco;
count++;
pessoas = (Pessoa **) realloc(pessoas, count*sizeof(Pessoa *));
pessoas[count-1] = new Pessoa(key, nome, idade, endereco);
bTree->add(pessoas[count-1]);
break;
case 's':
cin >> key;
toSearch = (Pessoa *) bTree->search(key);
if(toSearch == NULL)
{
cout << "-1" << endl;
}
else
{
cout << key << endl;
cout << toSearch->getNome() << endl;
cout << toSearch->getIdade() << endl;
cout << toSearch->getEndereco() << endl;
}
break;
case 'e':
done = true;
break;
}
} while(!done);
...
...
我有一个“菜单”,当我输入i 时,它将插入BTree,s 将搜索,e 将退出程序。
问题是,当我点击i插入时,我必须给四个参数:
- (int) 密钥;
- (字符串)名称;
- (int) 年龄;
- (字符串)地址;
当我给出一个包含e 字符的名称(例如“James”)时,它将退出程序。
如何避免command 在切换时读取标准输入缓冲区?
示例输入:
i
1
Joao da Silva 1
11
Rua 2, 3
i
2
Joao da Silva 2
12
Rua 4, 6
s
1
s
7
e
谢谢。
【问题讨论】:
-
有什么理由不使用
std::vector代替pessoas? -
这跟我的问题有关系吗?如果我只需要一个 realloc,为什么还要浪费堆栈?
-
还没有发现任何错误,只是想知道你为什么不在那里写惯用的 C++。
-
command是char吗? -
“这跟我的问题有关系吗?如果我只需要一个 realloc,为什么还要浪费堆栈?” ---(1)这与问题无关,(2)关于“浪费堆栈”的说法毫无意义。始终使用
std::vector。