Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - Looking For "help"
    primarykey
    data
    text
    <p>Okay, so I've actually been programming in C++ for quite a while now, but I am currently stumped on something that's probably <em>very</em> obvious. I decided to write a basic calculator for fun. Addition, subtraction, multiplication, division, the whole lot. As you can see below, I have an int variable called choice looking for either 1, 2, 3, or 4. Once chosen, it will call the corresponding function. However, I decided I want the ability to, at any time, type "help" to show help. How can I do this? I know I could simply make choice a string, but I feel that would just be putting a bandaid on the problem (Wouldn't help for future problems). I want to, at any time, catch a "help". However, using another if() statement to catch "help" would obviously give me an error - as choice is an int.</p> <p>Please help me, I am sure this is very simple, but for some reason I can't figure it out!</p> <pre><code>#include &lt;iostream&gt; int firstnum; int secondnum; int multiplication(){ std::cout &lt;&lt; "Multiplication chosen. Please enter first number." &lt;&lt; std::endl; std::cin &gt;&gt; firstnum; std::cout &lt;&lt; "Please enter second number." &lt;&lt; endl; std::cin &gt;&gt; secondnum; std::cout &lt;&lt; "Your answer is: " &lt;&lt; firstnum * secondnum &lt;&lt; "." &lt;&lt; std::endl; } int division(){ std::cout &lt;&lt; "Division chosen. Please enter first number." &lt;&lt; std::endl; std::cin &gt;&gt; firstnum; std::cout &lt;&lt; "Please enter second number." &lt;&lt; std::endl; std::cin &gt;&gt; secondnum; std::cout &lt;&lt; "Your answer is: " &lt;&lt; firstnum / secondnum &lt;&lt; "." &lt;&lt; std::endl; } int addition(){ std::cout &lt;&lt; "Addition chosen. Please enter first number." &lt;&lt; std::endl; std::cin &gt;&gt; firstnum; std::cout &lt;&lt; "Please enter second number." &lt;&lt; std::endl; std::cin &gt;&gt; secondnum; std::cout &lt;&lt; "Your answer is: " &lt;&lt; firstnum + secondnum &lt;&lt; "." &lt;&lt; std::endl; } int subtraction(){ std::cout &lt;&lt; "Subtraction chosen. Please enter first number." &lt;&lt; std::endl; std::cin &gt;&gt; firstnum; std::cout &lt;&lt; "Please enter second number." &lt;&lt; std::endl; std::cin &gt;&gt; secondnum; std::cout &lt;&lt; "Your answer is: " &lt;&lt; firstnum - secondnum &lt;&lt; "." &lt;&lt; std::endl; } int main(){ int choice; std::cout &lt;&lt; "Calculator." &lt;&lt; std::endl; std::cout &lt;&lt; "Multiplication: 1. Division: 2. Addition: 3. Subtraction: 4. Help: help." &lt;&lt; std::endl; std::cin &gt;&gt; choice; if(choice == 1){ multiplication(); } if(choice == 2){ division(); } if(choice == 3){ addition(); } if(choice == 4){ subtraction(); } ////if the user types "help" it will show help. return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload