Nested loops for menus (basic c++)
I'm running into errors where they say that a, q, h, etc aren't declared
variables, but I'm not sure how to fix this. Here is an example of my
code.
I'm also getting an error where it doesn't recognize userinp inside the
while loop, but I'm not sure how to transfer the variable storage from
outside the while loop to inside the while loop without declaring a global
variable, which I want to avoid.
Last question: I want to be able to have any key immediately return to the
main menu after each menu item, but I'm not sure what to use for that.
Any help is greatly appreciated!
//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;
}
}
}
No comments:
Post a Comment