0
#include <iostream>
#include <string>
#include "Python.h"
using namespace std;

#define py PyRun_SimpleString(pythonCommand.c_str())

int main()
{
	string pythonCommand;

	//Python started
	Py_Initialize();
		
		pythonCommand = "import sys";py;
		pythonCommand = "from math import sqrt";py;
		pythonCommand = "sys.setrecursionlimit(1000000)";py;
			//factorial function
		pythonCommand = "def ft(x): return(1 if x==0 else x * ft(x-1))";py;
			//equation using function
		pythonCommand = "print(sqrt(((ft(2008) + ft(2009) + ft(2010))/(ft(2008)))))";py;

	//Python ended
	Py_Finalize();

	cin.get();
	return 0;
}

In the above code, everything works just fine. Now I want to expand on it. I want to take the number I get from Python and move it back into C++ and then manipulate it in C++. However, I'm not sure how to go about doing that, any ideas?

I tried outputting the string after ending the python, however, that just outputted the string itself, i.e. (print(xyz)) was all that was output by C++.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ebrutekim
0

I hope its not illegal to bump this thread...

Seriously, does anyone have any idea how to do this? Should this maybe be posted in the Python forum?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.