r2 - 30 Aug 2010 - 11:21:28 - RajMathurYou are here: TWiki >  Main Web > ProgrammingIntroduction > 2010-08-25Wednesday

Topics covered on Wednesday, 2010-08-25

Command-line Arguments

Print out count and list of command-line arguments (arg0.cpp)

     1  //
     2  // arg0.cpp
     3  //
     4  // Print out the command line arguments
     5  // Copyright 2010, Raj Mathur
     6  //
     7  #include <iostream>
     8  #include <cstdlib>
     9
    10  using namespace std;
    11
    12  int main( int argc, char *argv[] )
    13  {
    14     cout << "You gave me " << argc << " arguments" << endl;
    15     cout << "I am " << argv[0] << endl;
    16     for( int i = 1; i < argc; i++ )
    17     {
    18        cout << "Argument: <" << argv[i] << ">" << endl;
    19     }
    20     exit(0);
    21  }

Print out the characters in the first command-line argument, one per line (arg.cpp)

     1  //
     2  // arg.cpp
     3  //
     4  // Print out the characters in the command-line argument, one per line
     5  // Copyright 2010, Raj Mathur
     6  //
     7  #include <iostream>
     8  #include <cstdlib>
     9
    10  using namespace std;
    11
    12  int main( int argc, char *argv[] )
    13  {
    14    if( argc != 2 )
    15    {
    16      cerr << "Usage: " << argv[0] << " argument" << endl;
    17      exit(1);
    18    }
    19    for( int i = 0; argv[1][i] != '\0' ; i++ )
    20    {
    21      cout << argv[1][i] << endl;
    22    }
    23    exit(0);
    24  }

Print out sum of two numbers given on the command line (sum2.cpp)

     1  //
     2  // sum2.cpp
     3  //
     4  // Print out the sum of the two numbers on the command line
     5  // Do not check for errors.
     6  // Copyright 2010, Raj Mathur
     7  //
     8  #include <iostream>
     9  #include <cstdlib>
    10
    11  using namespace std;
    12
    13  int main( int argc, char *argv[] )
    14  {
    15     if( argc != 3 )
    16     {
    17        cerr << "Usage: " << argv[0] << " num1 num2" << endl;
    18        exit(1);
    19     }
    20     int
    21        num1;
    22     int
    23        num2;
    24     int
    25        result;
    26     // Convert to int
    27     num1 = atoi(argv[1]);
    28     num2 = atoi(argv[2]);
    29    
    30     result = num1 + num2;
    31     cout << result << endl;
    32     exit(0);
    33  }

Assignment

sum3.cpp: Add any amount of numbers

Modify sum2.cpp above so that it prints out the sum of any amount of numbers given on the command line. Sample:

  ./sum3 2 10 4 14
  The sum is 30
Hint: Use a for loop to get the numbers one by one.

minmax.cpp: Find minimum and maximum of numbers

Create minmax.cpp which prints out the smallest and largest numbers given on the command line. Sample:
  ./minmax -5 10 -15 20 -25 30 50 -20 -10
  The smallest number is -25 and the largest is 50
Hint: Initialise two variables min and max to the first number. Then compare each of the other numbers with min and max and replace if lower/higher.

bank.cpp: Bank account withdrawal and deposit

Create bank.cpp which will allow you to deposit and withdraw cash. Sample (values in bold are user inputs):
  1. You have Rs. 0 in the bank.
  2. What would you like to do, (d)eposit or (w)ithdraw cash? w
  3. Sorry, can't withdraw from an empty account. Put some money in first.
  4. You have Rs. 0 in the bank.
  5. What would you like to do, (d)eposit or (w)ithdraw cash? d
  6. Amount of deposit? 10
  7. You have Rs. 10 in the bank.
  8. What would you like to do, (d)eposit or (w)ithdraw cash? w
  9. Amount of withdrawal? 20
  10. Overdraft facility not available yet. Please enter a sum less than Rs. 10
  11. Amount of withdrawal? 8
  12. You have Rs. 2 in the bank.
  13. What would you like to do, (d)eposit or (w)ithdraw cash? w
  14. Amount of deposit? 5
  15. You have Rs. 7 in the bank.
  16. What would you like to do, (d)eposit or (w)ithdraw cash? w
  17. Amount of withdrawal? 0
  18. Bye.
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions

tip TWiki Tip of the Day
SlideShowPlugin for presentations
Use the SlideShowPlugin to convert a topic with headings and bullets into a slideshow presentation. This ... Read on Read more

 
Kandalaya
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback