Is it possible to execute a string as code? Like for example, if the program asks the user to enter c++ code. The program can get the input as string, but is it possible to actually execute the code the user entered? If yes, how?
Not easily since C++ is meant to be compiled.
You have two ways of achieving this:
- Get the code, add what is needed to make it a compilable program, call a compiler, compile it, execute the output program
- Write a C++ interpreter ( or use an existing one )
You probably don't want them entering in C++ code, but you can embed a Python, JavaScript or Lua interpreter in a C++ program. Those can execute user-supplied code safely.
The documentation for embedding Google's V8 JavaScript engine is pretty easy to follow.