Difference between revisions of "CPP/CommandLineArguments"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==CommandLineArguments.cpp== <source lang="cpp"> #include <iostream> #include <string> #include <sstream> using namespace std; //test with //24.5 90.3 int main(int argc, char …')
 
m (CommandLineArguments.cpp)
Line 14: Line 14:
 
   int NumArgs = argc - 1;
 
   int NumArgs = argc - 1;
 
   cout << "Number of arguments: " << NumArgs << endl;
 
   cout << "Number of arguments: " << NumArgs << endl;
    
+
 
 +
   //argv[0] is the EXE name & is always present.
 
   string FirstArgument = argv[1];
 
   string FirstArgument = argv[1];
 
   string SecondArgument = argv[2];
 
   string SecondArgument = argv[2];

Revision as of 10:54, 23 June 2010

CommandLineArguments.cpp

#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
//test with
//24.5 90.3
 
int main(int argc, char *argv[])
{
  int NumArgs = argc - 1;
  cout << "Number of arguments: " << NumArgs << endl;
 
  //argv[0] is the EXE name & is always present.
  string FirstArgument = argv[1];
  string SecondArgument = argv[2];
 
  cout << "FirstArgument: " << FirstArgument << endl;
  cout << "SecondArgument: " << SecondArgument << endl;
 
  stringstream ssArg1, ssArg2;
  ssArg1 << FirstArgument;
  ssArg2 << SecondArgument;
 
  double dArg1, dArg2;
  ssArg1 >> dArg1;
  ssArg2 >> dArg2;
 
  cout << "FirstArgument: " << dArg1 << endl;
  cout << "SecondArgument: " << dArg2 << endl;
 
  std::string test = argv[1];
  cout << "argv[1]: " << argv[1] << endl;
  cout << "argv[1] string: " << test << endl;
 
  return 0;
}