Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an integer variable from one C++ program to another 1

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
I need to pass a single digit integer that the user enters from one program to another. Here is an example of what I need, I just don't know how to do it

#include<iostream>
using namespace std;
int numberToPass = 0;
int main()
{
cout << &quot;Please enter a number&quot; << endl;
cin >> numberToPass;
// pass the number to another program and
// execute the program
return 0;
}


#include<iostream>
using namespace std;
int numberToReceive = 0;
int main()
{
cout << &quot;The number you passed was &quot;;
cout << numberToReceive;
return 0;
}

I'm not sure if this can be passed directly, or with _pipe, or what... Please help!

 
One way to do this is to pass the # as an argument. Then the called program can convert the string argument into the actual #.

//This would be your main program. The program to recieve the argument is named argsPass.exe :

#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
std::string s=&quot;argsPass.exe &quot;;
std::string sNum;

std::cout << &quot;Enter # to pass: &quot;;
std::cin >> sNum;

s+=sNum;
system( s.c_str() );

std::cout << &quot;Back in calling program.&quot; <<
std::endl;

return 0;
}

//Lets say the program below was named argsPass.exe. This would be the code to recieve the # argument:

#include <iostream>
#include <string>

int main( int argc,char* argv[] )
{
std::string s;
int i=0;

s=argv[1];

std::cout << &quot;argsPass.exe is now running.&quot; <<
std::endl;

i=atoi( s.c_str() );
std::cout << &quot;The # &quot; << i << &quot; was passed.&quot; <<
std::endl;

return 0;
}
Mike L.G.
mlg400@linuxmail.org
 
This was perfect, simple and EXACTLY what I needed, kudos and thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top