C++ 无限循环,返回问题
C++ Infinite-loop, Issue with returning
void phonebookmenu() {
phonebook ph;
string str;
cin.ignore();
cout << "PH> ";
getline(cin, str); //Input from user.
string buf; // Have a buffer string.
stringstream ss(str); // Insert the string into a stream.
vector<string> tokens; // Create vector to hold the words.
while (ss >> buf){
tokens.push_back(buf); //Adds all the words inside the vector.
}
while (true){
if (tokens[0] == "add"){
ph.add(tokens[1],tokens[2]);
}
else if(tokens[0] == "lookup"){
ph.lookup(tokens[1]);
}
else if(tokens[0] == "change"){
ph.change(tokens[1],tokens[2]);
}
else if(tokens[0] == "alias"){
ph.alias(tokens[1],tokens[2]);
}
else if(tokens[0] == "quit"){
//Return to the "Main-menu"
}
else{
cout << "Invalid input" << endl;
}
}
所以我从"主菜单"调用此菜单,但在我输入"添加 peter 123"之类的内容后,它会执行该功能,然后返回到我不想要的"主菜单"。它应该回到
cout << "PH> ";
所以我可以继续做手术。
循环不围绕cout/input/determine/do something
逻辑。它只在determine/do something
附近.将while
移到cout
之前,看看会发生什么。请务必将最有帮助的答案标记为答案。
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 从 C++ 中的函数返回数组地址问题
- 在类中返回结构时出现问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 这个返回元素位置的基于循环的函数有什么问题?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 特征返回类型的 pybind11 问题
- 类的大问题,以及从空函数c++返回
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 存储另一个函数返回的布尔数组时遇到问题
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- 从 C++ 中的函数返回双精度的问题
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 字符 * 未从重载运算符或内存管理问题正确返回
- 类型转换问题:返回为整数而不是浮点/类型
- 反转具有返回值的字符串时出现问题
- C++问题返回通过引用本地创建的对象
- 简单c++程序Id的问题返回1退出状态
- 链接器问题返回未定义的引用