cpp_code = """
...
...
"""
import os
import sys
def compile_and_run_cpp(cpp_code):
cpp_filename = "main.cpp"
executable = "./a.out"
with open(cpp_filename, 'w') as file:
file.write(cpp_code)
compile_command = f"g++ {cpp_filename} -o {executable}"
compile_result = os.system(compile_command)
if compile_result != 0:
print(f"Compile Error: Compilation failed", file=sys.stderr)
sys.exit(1)
run_command = f"{executable}"
run_result = os.system(run_command)
if run_result != 0:
print(f"Runtime Error: Execution failed", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
compile_and_run_cpp(cpp_code)