c++ - How to use strings with switch
问题描述
I am brand new to programming. I have only learned up to functions in c++ so far, so please answer with in the scope of my knowledge.
I am working of a bank account program and I want to use a switch to get the user to input whether they want to deposit or withdraw.
I know I can use an int or even a char and do some like “D” for deposit but that’s not what I want to do.
I want to able to use the whole string “deposit” with the switch statement.
Thank you!
解决方案
The switch statement in c++ only supports integer types or values that can be evaluated to an integer. At best you could write a function to evaluate your strings to an integer as mentioned here
ref: Evaluate a string with a switch in C++