Question: C++ and Python Integration Issues. C++ runs the menu for the program and python does the math and C++ calls of the python function for

C++ and Python Integration Issues. C++ runs the menu for the program and python does the math and C++ calls of the python function for the math. Every time I run the Multiplication table it doesn't loop but when I run Double Value it does. I think it might be something in my python code but I am not sure. Please help me see what i am doing wrong.

CPP code

#include #include #include #include #include

using namespace std;

/* Description: To call this function, simply pass the function name in Python that you wish to call. Example: callProcedure(\"printsomething\"); Output: Python will print on the screen: Hello from python! Return: None */ void CallProcedure(string pName) { char* procname = new char[pName.length() + 1]; std::strcpy(procname, pName.c_str());

Py_Initialize(); PyObject* my_module = PyImport_ImportModule(\"PythonCode\"); PyErr_Print(); PyObject* my_function = PyObject_GetAttrString(my_module, procname); PyObject* my_result = PyObject_CallObject(my_function, NULL); Py_Finalize();

delete[] procname; }

/* Description: To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send Example: int x = callIntFunc(\"PrintMe\",\"Test\"); Output: Python will print on the screen: You sent me: Test Return: 100 is returned to the C++ */ int callIntFunc(string proc, string param) { char* procname = new char[proc.length() + 1]; std::strcpy(procname, proc.c_str());

char* paramval = new char[param.length() + 1]; std::strcpy(paramval, param.c_str());

PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr; // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyUnicode_FromString((char*)\"PythonCode\"); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, procname); if (PyCallable_Check(pFunc)) { pValue = Py_BuildValue(\"(z)\", paramval); PyErr_Print(); presult = PyObject_CallObject(pFunc, pValue); PyErr_Print(); } else { PyErr_Print(); } //printf(\"Result is %d \", _PyLong_AsInt(presult)); Py_DECREF(pValue); // Clean up Py_DECREF(pModule); Py_DECREF(pName); // Finish the Python Interpreter Py_Finalize();

// clean delete[] procname; delete[] paramval;

return _PyLong_AsInt(presult); }

/* Description: To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send Example: int x = callIntFunc(\"doublevalue\",5); Return: 25 is returned to the C++ */ int callIntFunc(string proc, int param) { char* procname = new char[proc.length() + 1]; std::strcpy(procname, proc.c_str());

PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr; // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyUnicode_FromString((char*)\"PythonCode\"); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, procname); if (PyCallable_Check(pFunc)) { pValue = Py_BuildValue(\"(i)\", param); PyErr_Print(); presult = PyObject_CallObject(pFunc, pValue); PyErr_Print(); } else { PyErr_Print(); } //printf(\"Result is %d \", _PyLong_AsInt(presult)); Py_DECREF(pValue); // Clean up Py_DECREF(pModule); Py_DECREF(pName); // Finish the Python Interpreter Py_Finalize();

// clean delete[] procname;

return _PyLong_AsInt(presult); }

void MultiplicationTable() { int a; cout cin >> a; cout }

void DoubleValue() { int b; cout cin >> b; cout cout }

void DisplayMenu() { int c; cout cout cout cout cin >> c; switch (c) { case 1: { MultiplicationTable(); DisplayMenu(); break; } case 2: { DoubleValue(); DisplayMenu(); break; } case 3: { cout break; } default: printf(\" Wrong Input \"); } }

void main() { DisplayMenu();

return;

}

Python Code

import re import string

def MultiplicationTable(a): n = 10 for i in range(1, (n+1)): q = a * i print(f\"{a} X {i} = {q}\")

def DoubleValue(b): return b * 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!