-1

Im in the process of trying to recreate a piece of python code for a simple calculator in C++,

in python i have the following piece of code thats located in a while loop

func = str(input("which function: add, sub, div, mult"))
if func in ("add", "sub", "div", "mult"):
   #secondery if statment

else:
   print("error")

how would i go about writing if func in ("add", "sub", "div", "mult"): in C++?

i have tried

if (func in {"add", "sub", "div", "mult"}){                
    //secondery if statment

else:
   std::cout << "error \n";

}

but this doesn't work.

Adam23262
  • 13
  • 2
  • No, you cannot do that like this because C++ and Python are two very different languages. You need to "manually" build the same check, by storing the strings in a container (e.g.: an array) then running a check if `func` is equal to any element stored inside that container – UnholySheep Nov 30 '22 at 14:57
  • What about `if (0 < (std::set{"add", "sub"}).count(func))` ? You can put the set in a non-local variable if you don't want to instantiate it every time the condition is executed. – Rulle Nov 30 '22 at 14:58
  • Did you [read a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Paul Sanders Nov 30 '22 at 14:58
  • Use std::set and either find or count – Severin Pappadeux Nov 30 '22 at 15:02
  • 2
    *Im in the process of trying to recreate a piece of python code for a simple calculator in C++* -- That is not how to "recreate" C++ from python code. The way to "recreate" the code is to actually know both C++ and python, take the python code, figure out what it's doing, then throw it away and implement the same thing in C++ using C++ idioms and best practices. Doing line-by-line translations from python to C++ will just end up with programs that are 1) buggy, 2) inefficient, or 3) look weird to a C++ programmer. – PaulMcKenzie Nov 30 '22 at 15:07

3 Answers3

2

C++17 shorthand version:

template<typename T, typename... Args>
[[nodiscard]] constexpr bool isAnyOf(T&& a, Args&&... args) noexcept
{
    return ((a == args) || ...);
}

Note this will work only if there is expected equal operator between first argument and each remaining argument.
So in case you have type char * as first argument and other argument it will compile but will not work properly since pointers will be compared not pointed strings.

There is also C++11 std::any_of

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 2
    You should mention the restriction of having to pass `std::string` or `std::string_view` for the first or for all the other parameters. For `char func[] = "add"; if (isAnyOf(func, "add", "sub", "div", "mult"))` your function wouldn't work... – fabian Nov 30 '22 at 15:46
  • Since input variable `func` should be `std::string` I didn't think about that. – Marek R Nov 30 '22 at 17:00
0

Here's a working snippet

#include <iostream> // std::cout, std::cin
#include <string>
#include <array> // one of the many STL containers
#include <algorithm> // std::find

int main() {
    const std::array<std::string, 4> functions = { 
        "add",
        "sub",
        "div",
        "mult"
    };

    std::string user_input;
    std::cout << "which function: add, sub, div, mult? ";
    std::cin >> user_input;

    if (std::find(functions.begin(), functions.end(), user_input) != functions.end()) {
        std::cout << "found" << std::endl;
    } else {
        std::cout << "not found" << std::endl;
    }
}

If you want to dig more into Python's in operator equivalents there's this question with a great answer.

NOTE: If you want to know which string was found you should store the iterator returned by std::find() and dereference it when needed.

R1D3R175
  • 92
  • 7
  • 1
    Starting C++17 `std::array` could be used instead of an array of `std::string`; this would also allow for making the array `constexpr`... – fabian Nov 30 '22 at 15:41
0

sometime we forget the basic:

std::string str;
std::cin >> str;    
if(str == "add" || str == "sub" || str == "mult" || str == "sub")
{
    std::cout << " do good stuff ..";
} else std::cout << "error";