You are on page 1of 6

KT01103

ENGINEERING
PROGRAMMING
LABORATORY 1 Getting Started in C++
SCHOOL OF ENGINEERING AND
INFORMATION TECHNOLOGY, UNIVERSITI
MALAYSIA SABAH
Student Name
Matrix No:

1

Experiment 1.1: Writing a C++ program file

1. Open up the Dev-C++.
2. On the top menu go to File->New->Source File.
3. Go to File->Save as. Save the File by using any name (example: yourName_L1).
5. Save the File where ever you want (you can also create a dedicated Folder for all C++ program files).
6. Write the following in the editor (the soft blue line):

#include <iostream>

using namespace std;

int main()
{
cout << "Hello..." << endl;
cout << "Welcome to C++ Programming." << endl;
cout << "Its fun, you will love it." << endl;

system("pause");
return 0;
}

7. Now, save the file.
8. Then on the top menu go to execute->compile & run (or else just press F9).
9. If everything was successful, a command prompt window should pop up and display the output. If
not, check your program carefully. Record the output in the below space:







10. Insert double slash (//) in front of Preprocessor commands #include <iostream> as such //#include
<iostream>). In C++, anything that written after double slash will be ignored by the compiler. Press
F9. Observe your finding. What is the function of #include <iostream>?









2

11. Remove // in step 10. Insert // in front of using namespace std. Press F9. Observe. What is the
function of using namespace std?







12. Remove // in step 11. Change int main() to int main1(). Press F9. Observe. Why you need one and
only main() function in your program?







13. Correct the error introduced in step 12. Insert // in front of system (pause). Press F9. Record your
observation.







14. Correct the error introduced in step 13. Write the standard structure of C++ program in the following
box.



















3

Experiment 1.2: The compiling process and Error Reporting

This experiment introduces the compiling process and the manner in which your compiler reports the
errors that it finds. Error messages vary greatly from one system to another. Some systems are very
helpful to the programmer, and some are not. Most C++ compilers pinpoint the line where the error has
occurred and also display a short error message. It is then up to you to play the part of detective, going
back into the source program and searching for the error yourself.

15. Append the program in Experiment 1.1 with the following lines:

cout << "C++ is easy. \n";
cout << "Believe me! \n";

16. Press F9. If the compiler finds errors (which would be typing errors), correct them and try again.
Execute the final, correct program. What happens?







17. In case you didn't have any typing errors on your first attempt, we'll introduce some now. Change the
word cout in the source program to couts and try to compile the altered version. How does your
compiler inform you of this error?







18. Correct the error introduced in 17, and then remove the semicolon from the end of the line:

cout << "C++ is easy. \n";

Try to compile this altered version (press F9). How does your compiler respond?












4


19. Correct the error introduced in 18, and then remove the closing brace } at the end of the program. Try
to compile this altered version. How does your compiler respond? Why does the compiler produces
error responds?








20. Correct the error introduced in 19. Insert the following lines:

cout << "Sekolah Kejuruteraan Teknologi Maklumat!\n";
cout << "Sekolah Kejuruteraan\n Teknologi Maklumat!";
cout << "Sekolah Kejuruteraan \n\nTeknologi Maklumat?";

What does the \n character combination mean?









Experiment 1.3: Displaying and creating an art

21. Open a new source file (File->New->Source File). Write a program that contains the following lines.
Press F9 and observe the output.

string s1 = " * * * * * * ";
string s2 = " * * * * * ";
string s3 = "__________________________________\n";
string s4 = "_________________________________________________________\n";

cout << s4 << s1 << s3 << s2 << s3;
cout << s1 << s3 << s2 << s3;
cout << s1 << s3 << s2 << s3;
cout << s1 << s3 << s2 << s3;
cout << s4 << s4 << s4 << s4 << s4;












5

22. Using the idea given in 21, create a Malaysian flag by using your own creativity. Write your program
and paste the output here (use print screen to copy and paste the output).

You might also like