菜单的嵌套循环(基本 c++)
Nested loops for menus (basic c++)
我遇到了错误,他们说a,q,h等不是声明的变量,但我不确定如何解决这个问题。这是我的代码示例。
我也收到一个错误,它无法识别 while 循环中的 userinp,但我不确定如何在不声明全局变量的情况下将变量存储从 while 循环外部转移到 while 循环内部,我想避免这种情况。
最后一个问题:我希望能够在每个菜单项之后立即将任何键返回到主菜单,但我不确定该使用什么。
任何帮助将不胜感激!
//Runs a program with a menu that the user can navigate through different options with via text input
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
int userinp;
cout<<"Here is the menu:" << endl;
cout<<"Help(H) addIntegers(A) subDoubles(D) Quit(Q)";
cin >> userinp;
userinp = tolower(userinp);
while userinp != q //loop while no q is input
{
if userinp == h
{
cout <<"This is the help menu. Upon returning to the main menu, input A or a to add 2 intergers." << endl;
cout <<"Input D or d to subtract 2 doubles. Input Q or q to quit.";
}
if userinp == a
{
int a, b, result;
cout <<"Enter two integers:";
cin << a << b;
result = a + b;
cout << "The sum of " << a << " + " << b << " = " << result;
}
}
}
试试这个:
while(userinp != 'q') {
而这个:
if(userinp == 'h')
而这个:
if(userinp == 'a')
等等,您可能希望将userinp
声明为char
而不是int
。
如果要比较文字字符,则必须使用单引号。 如果要比较文本字符串,则需要双引号。 唯一不使用引号的比较文字数字。
您的输入也有错误。 您的>>
应该是<<
。将>>
与cin
一起使用,将<<
与cout
一起使用。
代码问题:你希望用户inp是一个字符,但你已经声明为整数2)如果条件应在括号内
相关文章:
- C++嵌套if语句,基本货币交换
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 我似乎对if/else的基本语句有问题:/
- llvm构建器向基本块添加终止符
- "std::unique_XXX"命名约定背后的基本原理是什么?
- 如何将这个std::字符串转换为std::基本字符串
- 将带有派生模板的对象传递给接受具有基本模板的对象的功能
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 通过智能指针和转换对基本模板参数进行模板推导
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 当覆盖存在时调用基本虚拟"binded to object"函数
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- std::背后的基本原理assignable_from可能的实现
- 如何检查模板专用化是否是基本模板的子类?
- 为什么这个递归没有得到它的基本情况?
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 基本 在C++生成器中输入关键功能
- 根据模板的基本类型创建不同的模板类
- 如何在新的派生对象中获取基本对象的数据?
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误