![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
is the return statement. The return statement directs a function to return to its caller; in this context, main returns to the operating system. A return expression can optionally return a value to its caller. In this instance, the return statement returns a zero to the operating system. Thats all that is required for a C++ program. In this exercise, you have built a very simple, yet straightforward C++ program. All C++ programs must follow this template. The remainder of a programs functionality is up to you to develop. This book consists of many examples you need to expand on this basic example. Comments Source code style is a hotly debated topic. Every programmer develops his or her own style of source code formatting. The sample that follows is the traditional K&R style: if( number < 100 ) { // perform some functionality } The following demonstrates block style: if( number < 100 ) { // perform some functionality } The following sample is a variation of the block style and is known as indented block style: if( number < 100 ) { // perform some functionality } The most widely accepted style is the second form, block style. Block style is more visually pleasing and the braces are easier to visually align. The convention used for the filename identified in the #include complies with the Standard C++ convention. No extension is specifically identified for the filename. If your compiler complained that it could not open iostream, try replacing the statement: #include <iostream> with the following: #include <iostream.h> If your compiler does not conform to the new convention, it will try to find a file named iostream. Changing the name to iostream.h should help. If your compiler still complains with No such file or directory, you have one of two problems. Either you have a very old compiler or your compilers settings are incorrect. If you are using a very old compiler, I strongly suggest that you upgrade to one of the newer packages. Why? The examples throughout this book assume you are using a current compiler packageolder compilers might not understand some of the constructs used. I also mentioned that the compilers settings might be incorrect. Specifically, check the settings that the compiler uses for file locations, such as path settings or environment variables. If your compiler complains about the following source code line: using namespace std ; simply remove that line from the source file and recompile. If the compiler no longer complains, it does not support the namespace keyword. Alternatively, you can precede the text with the line comment // characters, as in the following example: //using namespace std ; Remember that the compiler will ignore any source code beyond the line comment sequence. It is the decision of the compiler vendor whether to implement a language feature or not; the only way to know is to try the feature or check the vendors documentation. The optional declaration for main is the following: int main( int argc, char *argv[] ) ; The first argument, int argc, represents the number of command-line arguments provided to the program. The second argument, char *argv[], is an array of pointers to char (specifically, to each of the arguments). Each argument to a C++ program is treated as a string of text. You can pass information to a program using arguments on the command line. For example, the following shows a program that accepts input to multiply two numbers: Prompt> mult 114 23 This example will read the two arguments, multiply the two values, and display the result to the standard output. 1.2 Create a program to perform calculations?Problem I would like to write a program that can perform some calculations. I know that I will have to use data variables, but I am unfamiliar with the various data types and operators in C++. Technique Programming languages provide data type variables that can be used to hold values. A value can be stored, accessed, and replaced in a variable. In order to use a variable, you must decide the data type(s) that you need. The C++ language defines a number of variable data types to choose from. The program presented in this How-To will calculate the number of inches for a specified number of feet. Steps
How It Works Lets review the source code and see what is happening. As in How-To 1.1, if your compiler complains about the following two lines of code: include <iostream> using namespace std ; change them to this one line of code: #include <iostream.h> The third line of code const int inchesInFoot = 12 ;
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|