California State University, Northridge



[pic]

|College of Engineering and Computer Science

Computer Science Department

Computer Science 106

Computing in Engineering and Science | |

| |Spring 2006 Class number 11672 Instructor: Larry Caretto |

Second Programming Exercise

Objective

This exercise gives you practice in writing simple C++ programs. It should familiarize you with the basic structure that you need to use for all your C++ programs as well as the basic approaches for getting output to the screen and getting input from the keyboard.

Background for exercise

From the first exercise, you should be familiar with the basic structure of a simple C++ program shown below.

#include

using namespace std;

int main()

{

// program statements go here

return EXIT_SUCCESS;

}

You will use this basic structure for all your C++ programs. The purpose of each statement in this basis structure is described below.

• The #include statement is used to include the library routines for keyboard input and screen output. Other libraries, which you will see in future programs, may be included by additional #include statements. In the first exercise we included the library to use standard math routines. Later in the course we will be using the libraries , , and , for file input-output, input-output formatting, and string variables, respectively.

• The using namespace std; command allows us to use all the standard C++ names. The concept of namespaces and variable scope will be discussed later in the course. For now, just take this statement as a requirement.

• The statement int main() is the header for the function main. All C++ programs start execution in this function. Most of the programs that you write in this course will have only this one function. Later in the course, you will see how to write a program that is composed of several functions.

• In the line, // program statements go here, the double slash (//) represents a comment. Such statements are ignored by the compiler and allow the programmer to place comments in the code that help a reader understand what is happening in the code.

• The statement, return EXIT_SUCCESS;, indicates that the program is halting after a successful execution. We will later learn that C++ functions use the return statement to return control and (optionally) to transmit a value to the function that called it. The main function returns a value to the operating system so that operating-system programs that call a sequence of C++ programs can test the success or failure of a given program. The quantity EXIT_SUCCESS is a global constant defined in the namespace std. We will later use global constant EXIT_FAILURE to terminate a program that does not execute successfully. The value of EXIT_SUCCESS is zero and you can simply write return 0; at the end of the program.

In addition to this basic program structure, you should include comment statements at the start of the program to describe its purpose and other information such as the programmer’s name and the date the program was written. See exercise one for examples of such comments.

In this exercise, you will use the basic structure to write a number of simple C++ programs. You can simply replace the line, // program statements go here, by the commands that you need.

Keyboard input and screen output in C++

Use the cout command with the output operator (sometimes called the insertion operator, ) for keyboard input. Here some basic rules.

• A single cout command can have one or more output ( ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download