You are on page 1of 23

CHAPTER 3

BASIC INPUT AND OUTPUT

OBJECTIVES:
At the end of the chapter the students should be able to: 1. Learn how to open, edit, and save simple C++ programs. 2. Describe the C++ input function and its parameters 3. Describe the C++ output function and its parameters 4. Use the C++ console output library command. 5. Use the C++ console input library command. 6. Enumerate common escape formatting characters.

43

3.1 Starting with MS Visual C++


In this section, we will be discussing and showing the basic operations to open, edit and save C++ programs using the MS Visual C++ IDE. To launch MS Visual C++ (Version 6.0) click on Start, then select Programs, and then select Microsoft Visual C++ 6.0.

Figure 3.1 Selecting Microsoft Visual C++ 6.0 The application will be launched and the following screen will be displayed with a splash screen Tip of the Day. It is recommended for beginners to read the tips, you may however opt to uncheck the Show tips at startup button if do not want to show tips upon startup. Click on the Close button to exit from the Tip of the Day screen.

44

Figure 3.2 Tip of the Day Splash Screen To create or edit a new C++ source code, click on the File menu and then select New.

Figure 3.3 Starting to Create a New Source Code

45
Upon selecting New from Figure 3.3 the application will respond by displaying a pop-up window. Click on the Files tab, select C++ Source File. You may specify the filename of the source code in the filename textbox as well as the location where you intend to save the file. Finally, click the OK button. You may however opt to skip this operation since you will be prompted to save your file before you can run it.

Figure 3.4 The New Pop-up Window


In the Figure below, you will see on top of the workspace the name of the file sampleprog.cpp. If you forgot to indicate the file name from Figure 3.4, C++ uses the default file name CPP1. You may then give a new name when you save the program. Thereafter, you can create or edit your C++ source code as shown in the figure below.

46

Figure 3.5 Creating/Editing a Source Code After creating your source code, you will now need to compile and link your program. This can be done by clicking on the Build menu and selecting Build.

Figure 3.6 Building the Source Code

47
If this is the first time that the source code is going to be compiled and linked, you will be shown the following pop-up window. Simply click on Yes.

Figure 3.7 Confirm Building the Source Code You will then need to save the file to continue compiling and linking the source code.

Figure 3.8 Saving the Source Code

48
Thereafter, the source code will be compiled and linked. If the program does not contain any syntax errors, you will see in the output window indicating 0 errors(s), 0 warning(s). Otherwise, you will need to fix any error(s) before you can run the program, or warning(s) to assure proper execution.

Figure 3.9 Compiling and Linking (Error Checking) the Source Code We are now ready of running or executing the program by simply clicking on the Build menu and Execute. The shortcut key combination of Ctrl+F5 also performs this operation.

49

Figure 3.10 Running/Executing the Program The output of the program will be shown in the console window as shown below.

Figure 3.11 Program Output

50
Upon pressing any key you will then return to the IDE. You may now opt to start another program or open an existing one. But before doing this, you must close the active or current program workspace. This can be done by clicking on the File menu and choosing Close Workspace as shown below.

Figure 3.12 Closing the Workspace A pop-up window will appear verifying if you intend to close all windows. Clicking Yes will close even the editor window, otherwise only the project workspace and the output window will be closed.

51

Figure 3.13 Close Workspace Confirmation Finally, if you prefer to exit the MS Visual C++ application, click on the File menu and choose Exit as shown below.

Figure 3.14 Exiting MS Visual C++

52

3.2 The Standard C++ Input/Output Commands


The C/C++ programming language by itself does not include commands for input/output of data. There is, however a pre-defined standard input/output library that can be linked with the programmers object file. Using such library commands require programmers to include the directive #include<iostream.h> in the beginning of their source code.

3.2.1 THE CONSOLE OUTPUT LIBRARY COMMAND


There are several library commands used for output of data, but well learn to use first the cout statement. The syntax is: cout [<< format string] [<< argument list] Where: cout refers to the console output library command << refers to the standard output stream operator (SOSO) the format string can be an ordinary character, string, or an escape character. the argument list can be a simple variable, constant, or an expression.

Below is an example program showing the use of cout.

53

#include <iostream.h> void main(void) { char ch; int i; float f; double d; // initialize variables ch = 'Z'; i = 70; f = 0.123456789; d = 0.123456789123456789; cout << "Hello\n"; // prints a string cout << 'A' << "\n" ; // prints a char constant cout << ch << "\n"; // prints the value of a char variable cout << 567 << "\n"; // prints an integer constant cout << i << "\n"; // prints the value of integer variable cout << 1.25 << "\n"; // prints a float constant cout << f << "\n"; // prints the value of float variable cout << 3.1416 << "\n"; // prints a double constant cout << d << "\n"; // prints a value of a double variable cout << "ch = " << ch << "\n"; // using different parameters cout << "i = " << i << " f = " << f << " d= " << d << "\n"; }

Source Code 3.1 Using cout

3.2.2 THE CONSOLE INPUT LIBRARY COMMAND


Like the output library commands, there are also several input library commands, but well learn to use first the cin statement. The syntax is: cin [>> argument list] Here, the argument list can only be variables, which will hold the data input of the user through the use of the keyboard. Below is a sample program illustrating the use of cin.

54

#include <iostream.h> void main(void) { char ch; int i; float f; double d; cin >> ch; cin >> i; cin >> f; cin >> d; // expects a character input from user // expects an integer input from user // expects a float input from user // expects a double input from user

// outputs the input values cout << ch = << ch << i = << i << f = << f << d = << d << \n; }

Source Code 3.2 Using cin Upon executing Source Code 3.2, expect only the cursor in the console window. Thus, it will be difficult to identify what the program expects from the user as input. To avoid such instances, it would be better to prompt the user how the program operates. Below is a modified version of Source Code 3.2 with prompts.

55
#include <iostream.h> void main(void) { char ch; int i; float f; double d; cout << "Input value of char variable ch: "; cin >> ch; cout << "Input value of int variable i: "; cin >> i; cout << "Input value of float variable f: "; cin >> f; cout << "Input value of double variable d: "; cin >> d; // outputs the input values cout << "ch = " << ch << " i = " << i << " f = " << f << " d = " << d << "\n"; }

Source Code 3.3 Using cin with Prompts

3.3 Special Formatting Characters


We have been using the \n in output statements from the previous section and noticed its effect upon running our sample programs. The \n character is just one of the special formatting characters of C++, it is referred to as the new line character. If used alone, the character must be enclosed in single quotes or in double quotes. It can also be used in conjunction with other characters in a format string enclosed in double quotes. There is, however, an alternative to the \n character that is more readable. Users can use the endl in place of \n, read as end line. But unlike the \n character, endl can not be used as part of a string, i.e enclosing it in double quotes or even single quotes. Doing so will cause unexpected results or a logical error. Below are the other special characters in C++.

56

Formatting Character \t \\ \ \

Description Generates the tab character to move the cursor to the next tab stop Prints the backslash ( \ ) Prints the single quote ( ) Prints the double quote ( ) Table 3.1 Common Formatting Characters

57

SUMMARY
In this chapter, we familiarize ourselves with the basic operations in using Microsoft Visual C++: opening, editing, saving, compiling, running, and closing a C++ program. We also discussed the basic input/output operations of the language and wrote simple programs with them.

58

PRACTICE EXERCISE #3
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

DIRECTION: Write in the boxes programs for the following. 1. Display the following using the cout statement. a. + ++ +++ ++++ +++++

59
b. Jose Rizal University Jose Rizal University Jose Rizal University Jose Rizal University Jose Rizal University

60
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

c. Display the initials of your name using the # character.

61
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

2. Prompt the user to input and 5 integer values and output the sum and average. Use the appropriate data type.

62
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

3. A department store gives 10% discount to all its shoes for sale. Prompt the user to input the cash price of a shoe and output the discount and the discounted price.

63
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

4. Prompt the user to input an integer value for N and output the result when used for formula: N(N + 1)(2N +1) 6

64
NAME: __________________________ COURSE/YR-SEC: ________________ DATE: _______________________ INSTRUCTOR: _______________

5. Compute for the Interest and Amount of: a. P1000 at 6% for 5 days b. P2000 at 6% for 10 days c. P3000 at 6% for 20 days Formula: Amount = Principal + Interest Interest = Principal * Rate * Time

You might also like