5. C and C Programming

Chapter 8 - Writing and Using Functions

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Command Line Arguments
Both C and C++ share the ability to accept command-line arguments. Command-line arguments are those arguments entered along with the program name when called from the operating system’s command line. This gives you the ability to pass arguments directly to your program without additional program prompts. For example, a program might pass four arguments from the command line:
YOURPROGRAM  Tia, ThinkingDog, Tango, BigDog
In this example, four values are passed from the command line to YOURPROGRAM. Actually, it is main( ) that is given specific information. One argument received by main( ), argc, is an integer giving the number of command-line terms plus 1. The program title is counted as the first term passed from the command line. The second argument is a pointer to an array of string pointers called argv. All arguments are strings of characters, so argv is of type char *[argc]. Since all programs have a name, argc is always 1 greater than the number of command-line arguments. In the following examples, you will learn different techniques for retrieving various data types from the command line. The argument names argc and argv are the commonly agreed upon variable names used in all C/C++ programs.
Alphanumeric
Arguments are passed from the command line as strings of characters, and thus they are the easiest to work with. In the next example, the C program expects that the user will enter several names on the command line. To ensure that the user enters several names, if argc isn’t greater than 2, the user will be returned to the command line with a reminder to try again.
/*
*   sargv.c
*   C program illustrates how to read string data
*   into the program with a command-line argument.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <process.h>

int main(int argc,char
*argv[])
{
 int t;

 if(argc<2) {
   printf(“Enter several names on the command line\n”);
   printf(“when executing this program!\n”);
   printf(“Please try again.\n”);
   exit(0);
 }

 for (t=1; t<argc; t++)
   printf(“Entry #%d is %s\n”,t,argv[t]);

 return (0);
}
You might have noticed that this program is completely contained in main( ) and does not use additional functions. The names entered on the command line are printed to the screen in the same order. If numeric values are entered on the command line, they will be interpreted as an ASCII string of individual characters and must be printed as such.
Integral
It is often desirable to be able to enter integer numbers on the command line, perhaps in a program that would find the average of a student’s test scores. In such a case, the ASCII character information must be converted to an integer value. The C++ example in this section will accept a single integer number on the command line. Since the number is actually a character string, it will be converted to an integer with the atoi( ) library function. The command-line value ivalue is passed to a function used earlier, called vbinary( ). The function will convert the number in ivalue to a string of binary digits and print them to the screen. When control is returned to main( ), the ivalue will be printed in octal and hexadecimal formats.
//
//  iargv.cpp
//  C++ program illustrates how to read an integer
//  into the program with a command-line argument.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <stdlib.h>
#include <process.h>

void vbinary(int idigits);

int main(int argc, char
*argv[])
{
 int ivalue;

 if(argc!=2) {
   cout << “Enter a decimal number on the command line.\n”;
   cout << “It will be converted to binary, octal and\n”;
   cout << “hexadecimal.\n”;
   exit(1);
 }
 ivalue=atoi(argv[1]);
 vbinary(ivalue);
 cout << “The octal value is: ” << oct
      << ivalue << endl;
 cout << “The hexadecimal value is: ”
      << hex << ivalue << endl;

 return (0);
}

void vbinary(int idigits)
{
 int t=0;
 int iyourarray[50];

 while (idigits != 0) {
   iyourarray[t]=(idigits % 2);
   idigits/=2;
   t++;
 }

 t—;
 cout << “The binary value is: ”;
 for(;t>=0;t—)
   cout << dec << iyourarray[t];
   cout << endl;
}
You might be interested in the formatting of the various numbers. You learned earlier that the binary number is saved in the array and printed one digit at a time, using decimal formatting, by unloading the array iyourarray in reverse order:
cout << dec << myarray[i];
To print the number in octal format, the statement is
cout << “The octal value is: ”
    << oct << ivalue << endl;
It is also possible to print the hexadecimal equivalent by substituting hex for oct, as shown here:
cout << “The hexadecimal value is: ”
    << hex << ivalue << endl;
Without additional formatting, the hexadecimal values a, b, c, d, e, and f are printed in lowercase. You’ll learn many formatting techniques for C++ in Chapters 11 and 12, including how to print those characters in uppercase.
Float
You will find that floats are just as easy as integers to intercept from the command-line. The following C example will allow several angles to be entered on the command line. The cosine of the angles will be extracted and printed to the screen. Since the angles are of type float, they can take on values such as 12.0, 45.78, 0.12345, or 15.
/*
*   fargv.c
*   C program illustrates how to read float data types
*   into the program with a command-line argument.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <math.h>
#include <process.h>
const double dPi=3.14159265359;

int main(int argc, char
*argv[])
{
 int t;
 double ddegree;

 if(argc<2) {
   printf(“Type several angles on the command line.\n”);
   printf(“Program will calculate and print\n”);
   printf(“the cosine of the angles entered.\n”);
   exit(1);
 }

 for (t=1; t<argc; t++) {
   ddegree=(double) atof(argv[t]);
   printf(“The cosine of %f is %15.14lf\n”,
          ddegree,cos((dPi/180.0)
*ddegree));
 }

 return (0);
}
The atof( ) function converts the command-line string argument to a float type. The program uses the cos( ) function within the printf( ) function to retrieve the cosine information.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net