8.7.1. pyramid.cpp, Version 2

Time: 00:09:18 | Download: Large Small | Streaming

This example demonstrates converting appropriately formed string objects and C-strings into numeric values. The example also provides another demonstration of using command-line arguments. We must recognize that argv is an array of character pointers, meaning that all command line arguments, even those the program processes as numbers, are C-strings.

We return to the pyramid.cpp program written in chapter 3 to provide the context for the current demonstration. The "real" problem-solving occurs in the earlier example, which you should review. In the previous version, we hard-coded the pyramid's height as a symbolic constant. In this version, we have the user enter the height of the pyramid on the command line. All command-line data enters the program as a C-string, so the first version converts the height entered on the command line from a C-string to an integer. The second version checks the command line for data input. If the user enters the height on the command, the program uses it; otherwise, it prompts the user to enter it through the console. The third and final version introduces an intermediate string object into the conversion process.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])				// (a)
{
	int height = atoi(argv[1]);				// (b)

	for (int level = 0; level < height; level++)
	{
		for (int spaces = 0; spaces < height - level - 1; spaces++)
			cout << ' ';
		for (int xes = 0; xes < 2 * level + 1; xes++)
			cout << 'X';
		cout << endl;
	}

	return 0;
}
Converting a C-string to an integer. The operating system passes all command line data into a program through a command line argument as a C-string. The program is responsible for converting arguments from C-strings to an appropriate type when needed.
  1. Accessing the command line arguments requires adding two variables to main
    • argc is the count or number of elements appearing on the command line
    • argv is an array or vector of C-strings - it contains all of the command line arguments, which are, as stated, C-strings.
  2. The atoi function converts a C-string in to an integer. The name "atoi" is short for ASCII to integer, where ASCII is often used to denote a C-string. It's a bit puzzling that the itoa function we used in cpalnumber.cpp is not a standard function while atoi is standard and widely available. You will find an extensive list of C-string to number functions at the top of the page.
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	int height;
if (argc == 2) height = atoi(argv[1]); else { cout << "Please enter the pyramid height: "; cin >> height; }
for (int level = 0; level < height; level++) { for (int spaces = 0; spaces < height - level - 1; spaces++) cout << ' '; for (int xes = 0; xes < 2 * level + 1; xes++) cout << 'X'; cout << endl; } return 0; }
Detecting the presence of command-line data. The variable argc is 1 when only the program name appears on the command line and the program has no arguments. When the program name and one argument are on the command line, then argc is 2. If the user enters the pyramid's height on the command line, the program uses it, but if the user does not provide a height on the command line, the program prompts the user to enter it.
#include <iostream>
#include <string>
using namespace std;

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

	if (argc == 2)
	{
		string st_height = argv[1];		// (a)
		height = stoi(st_height);		// (b)
	}
	else
	{
		cout << "Please enter the pyramid height: ";
		cin >> height;
	}

	for (int level = 0; level < height; level++)
	{
		for (int spaces = 0; spaces < height - level - 1; spaces++)
			cout << ' ';
		for (int xes = 0; xes < 2 * level + 1; xes++)
			cout << 'X';
		cout << endl;
	}

	return 0;
}
Converting a string object to an integer. This example is not an ideal demonstration of converting a string to a number because it requires an extraneous C-string-to-string conversion.
  1. The string conversion constructor converts a C-string, argv[1], into a string object.
  2. The string stoi function converts the string into a number. This function is one of many that convert strings to numeric values.