Reputation: 181
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;
}
}
}
Upvotes: 0
Views: 646
Reputation: 62052
Try this:
while(userinp != 'q') {
And this:
if(userinp == 'h')
And this:
if(userinp == 'a')
etc., and you probably want to declare userinp
as a char
rather than an int
.
If you want to compare a literal character, you must use single quotes. If you want to compare a literal string, you need double quotes. The only compare literal numbers without using quotes.
You also have an error with your input. Your >>
should be <<
. Use >>
with cin
and use <<
with cout
.
Upvotes: 1
Reputation: 205
problems with code: u expect userinp to be a charachter but u have declared as integer 2)conditions for if should be within brackets
Upvotes: 0