I am using Unix shell to run my C++ executable files. The program I am writing requires having some values assigned to its variables. I want to assign values to these variables on the command line rather than having a user manually entering them. In the following program, I was able to do that by providing two arguments in the command line.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
int apples, oranges;
apples = stoi(argv[1]);
oranges = stoi(argv[2]);
cout << "Total Fruits: " << apples + oranges << endl;
return 0;
}
However, if my program has a cin>>
line as shown in the following program, how can I assign values to apples
and oranges
variables without the user having to manually enter these values. I would like to assign values to cin>>apples
and cin>>oranges
at the command line run (similar to: program.exe 5 6
)
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
int apples, oranges;
cin >> apples;
cin >> oranges;
cout << "Total Fruits: " << apples + oranges << endl;
return 0;
}