20-CS-122-001 Computer Science II Spring 2012
Command Line Arguments

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Source

#include <iomanip>
#include <iostream>
using namespace std;

// Shows command line arguments, conversion to int, conversion to float
int main (int argc, char *argv[]) {
   char cmd[1024];
   cout << "argc = " << argc << "\n\n";
   for (int i=0 ; i < argc ; i++) {
      cout.setf(ios::left);

      sprintf(cmd, "argv[%d] = %s", i, argv[i]);
      cout << setw(24) << cmd;

      sprintf(cmd, "atoi(argv[%d]) = %d", i, atoi(argv[i]));
      cout << setw(24) << cmd;

      sprintf(cmd, "atof(argv[%d]) = %f", i, atof(argv[i]));
      cout << setw(24) << cmd << "\n";
   }
}