// arrayOfPointersDemo2.cpp
#include <iostream>
#include "textUtility2.h"
bool parseIntegers(char* integerTexts[],
int numbers[],
int length);
/*
* int main(int argc, char* argv[])
*
* Echoes any sequence of integers entered as
* command-line arguments.
*
* Command-line arguments:
* argv[0] - filename of this program's executable file.
* All subsequent argv[i] - any integers.
*
* Global variable:
* cout - an ostream object for output to the terminal.
* cout is an external variable, declared
* in library header file <iostream>.
*/
int main(int argc, char* argv[])
{
if ( argc < 2 ) {
cout << "Enter any sequence of integers as "
<< "command-line arguments." << endl;
cout << "This program will echo them." << endl;
return 0;
} // if
const int NUMBER_OF_NUMBERS = argc - 1;
int inputNumbers[NUMBER_OF_NUMBERS];
if ( ! parseIntegers(argv + 1,
inputNumbers,
NUMBER_OF_NUMBERS) )
{
cout << "You must enter integers only." << endl;
return 1;
} // if
cout << "You entered: ";
for ( int i = 0; i < NUMBER_OF_NUMBERS; i++ )
cout << " " << inputNumbers[i];
cout << endl;
return 0;
} // function main
/*
* bool parseIntegers(char* integerTexts[],
* int numbers[],
* int length)
*
* Generates the int values represented by
* a sequence of C-strings which represent
* integers.
*
* Parameters:
* integerTexts - an array of C-strings
* representing integers
* Precondition: Each element should
* represent an integer.
* Postcondition: unchanged.
* numbers - an array of the integers
* represented by integerTexts.
* Precondition: none
* Postconditions: Each element of
* numbers contains the integer
* represented by the corresponding
* element of integerTexts, if that
* element of integerTexts indeed
* represents an integer. Garbage
* otherwise.
* length - the length of both arrays
* integerTexts and numbers.
*
* Returns: true if all the elements of
* integerTexts represent integers,
* false otherwise.
*/
bool parseIntegers(char* integerTexts[],
int numbers[],
int length)
{
for ( int i = 0; i < length; i++ )
if ( ! parseForbinInt(integerTexts[i], numbers[i]) )
return false;
return true;
} // function parseIntegers