C++控制台不显示菜单
C++ console not showing menu
如果用户选择1或2,则函数不会运行。有什么建议吗?
#include <iostream>
using namespace std;
void getTitle();
void getIsbn();
int main()
{
int choice = 0; // Stores user's menu choice
do
{
// Display menu
cout << " Main Menunnn";
// Display menu items
cout << " 1. Choose 1 to enter Title.n";
cout << " 2. Choose 2 to enter ISBN.n";
cout << " 3. Choose 3 to exit.n";
// Display prompt and get user's choice
cout << " Enter your choice: ";
cin >> choice;
// Validate user's entry
while (choice < 1 || choice > 3)
{
cout << "n Please enter a number in the range 1 - 3. ";
cin >> choice;
}
switch (choice)
{
case 1:
getTitle();
break;
case 2:
getIsbn();
break;
}
} while (choice != 3);
return 0;
}
void getTitle()
{
string title;
cout << "nEnter a title: ";
getline(cin, title);
cout << "nTitle is " << title << "nnn";
}
void getIsbn()
{
string isbn;
cout << "nEnter an ISBN: ";
getline(cin, isbn);
cout << "nISBN is " << isbn << "nnn";
}
函数当然应该被调用。然而,会发生的情况是,当您按"Enter"键入数字时生成的换行符将由getline()
返回,并且该函数将在没有真正提示的情况下返回。你需要清除换行符。您可以使用ignore()
来做到这一点:在读取choice
后立即添加cin.ignore();
以忽略一个字符。
相关文章:
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 如何以编程方式显示功能区按钮的下拉菜单?
- 如何使用IExecuteCommand和动词在上下文菜单外壳扩展中显示本地化文本和自定义图标?
- QTool按钮按Alt后显示菜单
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 如何在所有者描述的 MFC CMenu 派生菜单中显示复选框?
- 无法在 cocos2dx 中显示菜单菜单项图像
- 将WM_CHANGEUISTATE发送到窗口应用程序后,当用鼠标访问菜单时,助记符不会显示在系统菜单上
- 如何手动显示CMFCToolBarComboBoxButton子菜单
- 如何为 QComboBox 的弹出菜单指定与框本身中显示的不同的字符串?
- TrackPopupMenu 显示的系统菜单与窗口状态不匹配
- 如何更新C 中已使用TrackPopupMenu显示的上下文菜单
- QT在菜单项上显示模态对话框(.UI)单击
- 切换菜单计算器将不显示算术
- 应用程序菜单中显示错误
- 如何在显示游戏菜单时处理无效输入
- 单击"菜单"时显示消息框未显示在屏幕上
- Qt C++ 菜单栏未显示在 Windows 上,而是在 Mac 上显示
- Mac OSX 上的 Eclipse CDT 未在首选项集中显示调试菜单
- 如何显示 Windows 资源管理器上下文(右键单击)菜单