ebrutekim 0 Newbie Poster
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include "Python.h"
using namespace std;

#define tP PyRun_SimpleString(pyCom.c_str())

double func(double x);

int main()
{	
	//file to write numbers to
	ofstream file;
	file.open("numCache.txt");
	
	//string for int to string conversion
	string temp;
	stringstream output;
	
	//string that is read from console
	string numCache;

	//returned number from python
	double retNum;

	double z;

	//python input
	string pyCom;
	Py_Initialize();

	for(int a = 1; a <= 10; a++)
	{
		//int to string conversion
		output << a;
		temp = output.str();
		
		//python activity
		pyCom = "x = " + temp;tP;
		pyCom = "print(x/5.0)";tP;
		
		//the problem lies here...
		getline(cin, numCache);
		
		//convert string to double
		retNum = atof(numCache.c_str());
		
		//run double through function
		z = func(retNum);
		file << z << endl;

		//clear the string stream
		output.str("");
	}
	
	Py_Finalize();
	file.close();

	cin.get();
	return 0;
}

//this could be any function
double func(double x)
{
	return ((x+3.5)/1.2);
}

I want to return something that was previously written to the console back into the program. Basically, here's how this program works: a number is generated, it is converted to a string, it is manipulated by python and then output onto the console. The next step is to read the line that was just output into a string (I believe) and then convert that string into a double and manipulate it in c++.

Any ideas on how to read the console?

As you can see I tried getline(cin,string), but I wouldn't be surprised if I was using that wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.