#switch case question

24 messages · Page 1 of 1 (latest)

heady grove
#

im a begginer making a calculator application and recently switched the mess of else if statements to one switch case. for some reason, now the 'e' option (supposed to exit the program) outputs the following:

select operation
([a]ddition, [s]ubtraction, [d]ivision, [m]ultiplication, [p]ower, [S]quare root, [e]xit)
e
type in 2 numbers (1 if sqrt): ```

this is the code.

``` #include <iostream>
#include <cmath>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

int calcs() {
    cout << "select operation" << endl << "([a]ddition, [s]ubtraction, [d]ivision, [m]ultiplication, [p]ower, [S]quare root, [e]xit)" << endl;
    char operation;
    cin >> operation;
    
    cout << "type in 2 numbers (1 if sqrt): ";
    double num1{ 0 }, num2{ 0 };
    cin >> num1 >> num2;
    cout << endl;
    
    switch(operation){
        case 'a':
            cout << num1 + num2 << endl << endl;
            break;

            //[here enters various cases (message is too long)]

        case 'e':
            return 0;

        default:
            std::cerr << "unknown command" << endl << endl;
            break;
        }
        return 1;
}

int main() {
    while (calcs()) {} //makes it so the program loops until user iputs 'e'
    return 0;
} ```
uncut heronBOT
#

@heady grove

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```cpp
int main() {}
```

Result
int main() {}
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

onyx forge
#

Because before you ever reach the switch statement, you have code asking for two inputs

#

The line after cin >> operation

scarlet pine
#

This isn’t a big deal but I don’t think the return 1 at the end of the function will ever be reached

#

Ah it can be reached the default is ran on the switch

scarlet pine
onyx forge
#

In you code
1.You ask for an operation.
2.Then you then ask for two numbers.
3.Then you check the operation.

It is only in step 3 where your code checks that e was inputed and returns. Step 2 happens regardless of what you input

heady grove
#

oohhh i see
so how can i make case e terminate the code?

onyx forge
#

Maybe have an if check just for e before your switch statement.

#

Use the switch statement just for when the user chooses addition/subtraction/etc

heady grove
#

that did the trick. thank you!

#

do i need to change the post to solved or smth

onyx forge
#

!solved or something like tgat

uncut heronBOT
#

@heady grove Has your question been resolved? If so, type !solved :)

heady grove
#

!solved

uncut heronBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

heady grove
#

thx

civic prism
# heady grove i dont understand

look you are doing

double num1{ 0 }, num2{ 0 };
 cin >> num1 >> num2;```
when is not neccessary, you should use num2 in another print,because you can type 2 numbers even without the num2,just saying it's bad code.
#

this doesn't effect anything just explaining to you

heady grove
#

hmm i see