Professional Documents
Culture Documents
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
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.
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.
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.
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.
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.
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.
52
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"; }
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"; }
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: _______________
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