Nexus Learn

Practical 1: Getting Started with C++: Your First Program

Lab Practical CS-307 Programming Fundamentals 10 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank (Difficult Words Made Easy)
  3. 3. Background: The Edit → Compile → Run Cycle
  4. 4. Time Plan
  5. Part A: Setting Up the IDE (20 min)
  6. Part B: Your First Program (15 min)
  7. Part C: Practise the Cycle (10 min)
  8. Part D: Bug Hunt (20 min)
  9. Part E: Graded Lab Tasks (45 min)
  10. Part F: Post-Lab Questions
  11. Marking Rubric (20 marks)
  12. Submission

Duration

Language

Tools

Linked Lecture

120 minutes

C++

Dev-C++ or Code::Blocks (online compiler as backup)

Lecture 1

Mapped outcomes: CLO1 (identify good problem-solving properties) and CLO4 (C++ syntax and semantics) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5

1. Lab Objectives

By the end of this lab, you will be able to:

  • Install and open a C++ IDE on a computer.
  • Write, save, compile and run a simple C++ program.
  • Explain what each line of a "Hello World" program does.
  • Read a compiler error message and fix the mistake.
  • Print neat, well-arranged text on the screen using cout, endl, \n and \t.

2. Word Bank (Difficult Words Made Easy)

Read these words first. You will see them many times in this lab.

Word

Simple meaning

Real-world example

Program

A list of instructions that tells the computer what to do.

A recipe that tells a cook what to do, step by step.

Source code

The program you type, written in a language like C++.

The recipe written on paper.

IDE

Integrated Development Environment: one software where you type, compile and run your code.

A kitchen that has the stove, knife and plates in one place.

Compiler

A tool that translates your C++ code into machine language (0s and 1s).

A translator who turns an English letter into Urdu so someone else can read it.

Compile

To translate code using the compiler.

Getting the letter translated.

Syntax

The grammar rules of a programming language.

In English, a sentence ends with a full stop. In C++, a statement ends with ;.

Error / Bug

A mistake in the program.

A spelling mistake in a job application.

Debug

To find and remove mistakes.

Proof-reading the application before sending it.

Output

What the program shows on the screen.

The receipt a shopkeeper prints for you.

Console

The black window where the output appears.

The screen of an ATM machine that shows messages.

3. Background: The Edit → Compile → Run Cycle

Every C++ program goes through the same three steps. You will repeat this cycle in every lab of this course.

Step

What you do

What the computer does

1. Edit

Type your code in the IDE and save it as a .cpp file.

Stores your source code on the disk.

2. Compile

Click the Compile button.

Checks the syntax. If there is no error, it creates an .exe file (machine code).

3. Run

Click the Run button.

Executes the .exe file and shows the output in the console.

Real-world example: Think of writing an exam answer in English for a teacher who reads only Urdu. You write the answer (edit), a translator checks and translates it (compile), and the teacher reads it (run). If your English has a grammar mistake, the translator sends it back to you. That is a compiler error.

4. Time Plan

Part

Activity

Time

A

Setting up the IDE

20 min

B

Your first program (Hello World)

15 min

C

Practise the edit-compile-run cycle

10 min

D

Bug hunt: find and fix errors

20 min

E

Graded lab tasks

45 min

F

Post-lab questions and submission

10 min

Part A: Setting Up the IDE (20 min)

Your lab computer may already have an IDE installed. Ask the lab attendant first. Follow one of the options below.

Option 1: Dev-C++ (recommended for beginners)

  1. Open Embarcadero Dev-C++ from the Start menu (or download it free from its official website on your home PC).
  2. Click File → New → Source File (shortcut: Ctrl + N).
  3. Click File → Save As. Choose your folder, and name the file lab1_hello.cpp. Make sure the type is C++ source files.
  4. Type your code.
  5. Press F9 to compile. Read the message at the bottom of the window.
  6. Press F10 to run. A black console window will open with your output.
  7. Shortcut: F11 = compile and run in one step.

Option 2: Code::Blocks

  1. Open Code::Blocks. (On a home PC, download the version whose name includes "mingw-setup" — it comes with a compiler.)
  2. Click File → New → Empty File, then save it as lab1_hello.cpp.
  3. Type your code.
  4. Click Build → Build and Run (shortcut: F9).
  5. Errors appear in the Build messages tab at the bottom.

Option 3: Online compiler (backup only)

If no IDE works, open an online C++ compiler in the browser, such as OnlineGDB or Programiz. Choose the language C++, type the code and press Run. Note: online compilers need the internet, and your file is not saved on the PC.

Good habit: Make a folder on the desktop named with your roll number, for example 2026-ag-1234_PF. Save every lab file in it. Name files clearly: lab1_task1.cpp, lab1_task2.cpp, and so on.

Part B: Your First Program (15 min)

Type the program below exactly as shown. Do not copy-paste. Typing helps you learn the syntax.

lab1_hello.cpp

// My first C++ program

#include <iostream>

using namespace std;

int main() {

cout << "Hello, UAF!" << endl;

cout << "I am learning C++." << endl;

return 0;

}

Output on screen

Hello, UAF!

I am learning C++.

What does each line mean?

Line of code

Meaning in simple words

// My first C++ program

A comment: a note for humans. The compiler ignores it.

#include <iostream>

Brings in the "input/output" library so we can use cout and cin. Like borrowing a tool box before starting work.

using namespace std;

Lets us write cout instead of the longer std::cout.

int main() {

The starting point of every C++ program. The computer always begins here. { opens the body.

cout << "Hello, UAF!"

cout (say "see-out") sends text to the screen. Text inside double quotes is printed exactly as written.

<< endl;

endl means "end line": move the cursor to the next line. ; ends the statement, like a full stop.

return 0;

Tells the computer "the program finished successfully".

}

Closes the body of main.

Remember: C++ is case-sensitive. cout and Cout are different words for the computer. Main is not the same as main.

Part C: Practise the Cycle (10 min)

Make each small change below in lab1_hello.cpp. After each change: save → compile → run, and write what you see.

#

Change to make

What happened?

1

Replace "Hello, UAF!" with your own name.

2

Delete << endl from the first cout line only.

3

Add a third cout line that prints your city.

4

Add cout << endl; between two lines.

Part D: Bug Hunt (20 min)

Each program below has one mistake. Type it, compile it, and read the error message carefully. The message tells you the line number and gives a clue. Then fix the bug and run the program again.

Tip for reading errors: An error such as b1.cpp:4:34: error: ... means: file b1.cpp, line 4, column 34. Sometimes the real mistake is on the line just before the line that the compiler shows.

Bug 1 (bug1.cpp)

#include <iostream>

using namespace std;

int main() {

cout << "Hello, UAF!" << endl

return 0;

}

Error message (copy the main part):

What was the mistake and how did you fix it?

Bug 2 (bug2.cpp)

#include <iostream>

using namespace std;

int main() {

Cout << "Welcome to the lab" << endl;

return 0;

}

Error message (copy the main part):

What was the mistake and how did you fix it?

Bug 3 (bug3.cpp)

#include <iostream>

using namespace std;

int main() {

cout << "Programming is fun << endl;

return 0;

}

Error message (copy the main part):

What was the mistake and how did you fix it?

Bug 4 (bug4.cpp)

using namespace std;

int main() {

cout << "Faisalabad" << endl;

return 0;

}

Error message (copy the main part):

What was the mistake and how did you fix it?

Bug 5 (bug5.cpp)

#include <iostream>

using namespace std;

int mian() {

cout << "Good morning" << endl;

return 0;

}

Error message (copy the main part):

What was the mistake and how did you fix it?

Part E: Graded Lab Tasks (45 min)

Two small new tools will help you arrange your output neatly. They are called escape sequences: a backslash \ followed by a letter, written inside the double quotes.

Escape sequence

Meaning

Example

Output

\n

New line (same job as endl)

cout << "Hi\nBye";

Hi (new line) Bye

\t

Tab: jumps to the next column (8 spaces wide)

cout << "A\tB";

A B

\"

Print a double-quote character

cout << "He said \"OK\"";

He said "OK"

Real-world example: The \t key works like the Tab key in MS Word. When you make a list of items and prices, Tab lines up the prices in a straight column.

Task 1: Student ID Card (Easy — 3 marks)

Write a program that prints your details, each on a separate line: full name, roll number, degree (BSCS), department, and university. Add a line of = signs above and below the card.

Save as: lab1_task1.cpp

Task 2: Shop Receipt (Medium — 4 marks)

A general store on Jail Road, Faisalabad wants a printed receipt. Write a program that prints the shop name, address, and a table of three items with Qty and Price. Use \t so the columns line up. Print the total at the end and a "Thank you" message.

Output on screen

==============================

AL-MADINA GENERAL STORE

Jail Road, Faisalabad

==============================

Item Qty Price

------------------------------

Milk 1L 2 460

Bread 1 150

Eggs (dozen) 1 420

------------------------------

Total Rs. 1030

==============================

Thank you! Come again.

Save as: lab1_task2.cpp

Task 3: Star Patterns (Medium — 4 marks)

Print the two star patterns shown below, with one blank line between them. Use only cout and endl.

Output on screen

*

**

***

****

*****

*

***

*****

*******

*********

Save as: lab1_task3.cpp

Task 4: My Class Timetable (Medium — 4 marks)

Print your weekly timetable with three columns: Day, Time, and Course. Use \t between columns. Include at least four days.

Output on screen

Day Time Course

Monday 08:00 Programming Fundamentals

Tuesday 10:00 Calculus

Wednesday 08:00 PF Lab

Thursday 12:00 English

Save as: lab1_task4.cpp

Task 5: Bus Ticket (Challenge — 5 marks)

Design a bus ticket from Faisalabad to Lahore inside a box made of +, - and | characters. Show From, To, Seat and Time. At the bottom, print the message "Have a safe journey!" with the double quotes visible on screen. Every line of the box must have the same width.

Output on screen

+--------------------------------+

| FSD EXPRESS TICKET |

+--------------------------------+

| From : Faisalabad |

| To : Lahore |

| Seat : 14 |

| Time : 09:30 AM |

+--------------------------------+

| "Have a safe journey!" |

+--------------------------------+

Save as: lab1_task5.cpp

Part F: Post-Lab Questions

Q1. What is the difference between a compiler error and a linker error? Give one example of each from this lab.

Q2. Why does every C++ program need a main() function?

Q3. What is the difference between endl and \n?

Q4. A student wrote cout << "Total" << endl and the compiler showed an error on the NEXT line. Why?

Q5. What will this print? cout << "A\tB\nC";

Marking Rubric (20 marks)

Component

Marks

What earns full marks

Task 1

3

All details on separate lines with borders.

Task 2

4

Correct receipt; columns lined up with \t.

Task 3

4

Both patterns exact, blank line between them.

Task 4

4

Three aligned columns, at least four days.

Task 5

5

Box with straight borders; quotes visible in the message.

Note: Bug hunt (Part D) and post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.

Submission

  • Show your running programs to the instructor before leaving the lab.
  • Put all five .cpp files (Task 1–5) in a folder named with your roll number, zip it, and submit it as instructed by your teacher.
  • Write answers to the bug hunt and post-lab questions in this handout and submit it.
  • Copying code from a classmate gets zero marks for both students. Discussing ideas is fine; copying code is not.

Next practical: Variables, data types, and taking input from the user with cin (linked to Lecture 2).

← All CS-307 material