Programming Fundamentals
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 13
Structures in C++
Muhammad Ashnab Safdar I24-6500 CE-A
STUDENT NAME ROLL NO SEC
______________________________________
LAB ENGINEER'S SIGNATURE & DATE
Muhammad Hammad
MARKS AWARDED: /10
_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir, Muhammad Hammad Version: 1.1
Date last
Last edited by: Engr. Azhar Rauf, Engr. Aamer Munir Sep 27, 2018
edited:
Date last
Verified by: Engr. Azhar Rauf Jan 10, 2025
edited:
Structures LAB 13
Lab Objectives:
1. To learn what are structures in C++
2. To learn how to define a structure and access its data members.
3. To learn about array of structures.
Software Required:
Dev C++
Introduction:
1. Structures in C++
Structure is the collection of variables of different types under a single name for
better visualization of problem. Arrays is also collection of data but arrays can hold
data of only one type whereas structure can hold data of one or more types.
1.1. How to declare a structure in C++ Programming?
The struct keyword declares a structure type followed by an identifier (name of
the structure). Then inside the curly braces, you can declare one or more
members (declare variables inside curly braces) of that structure, termed as
definition of structure. For example:
struct person {
char name[100]; //or string name;
int age;
float salary;
};
Here a structure person is declared which has three
members: name, age and salary.
When a structure is declared, no memory is allocated. How to declare a
structure variable?
Once you have declared a structure person as above, you can declare a structure
variable (or identifier) as:
person bill;
Here, a structure variable bill is defined which is of type structure person. When
structure variable is declared only then the required memory is allocated by the
compiler. The memory of float is 4 bytes, memory of int is 4 bytes and memory
of char is 1 byte. Hence, 108 bytes of memory is allocated for structure
variable bill.
1.2. How to access members of a structure?
PF LAB 13 NUCES, ISLAMABAD Page 2 of 19
Structures LAB 13
The member of structure variable is accessed using dot operator. Suppose, you want to
access age of structure variable bill and assign 50 to it. You can perform this task by
using following code below:
bill.age = 50;
Task01: Run the following example, and see its output
Given below is a structure person which has three members. Inside main() function, a
structure variable p1 is defined. Then, the user is asked to enter information and data
entered by user is displayed.
#include <iostream>
using namespace std;
struct person { // Defining a structure
char name[50];
int age;
float salary;
};
int main() {
person p1; // Declaring a struct variable
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout << "Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
It takes input of Name , age and salary of a person using structure variable and then
output it using again structures
PF LAB 13 NUCES, ISLAMABAD Page 3 of 19
Structures LAB 13
Task02: Write a program that can compare two dates entered by the user. Make a
structure named Date to store the elements day, month and year to store the dates. If the
dates are equal, display "Dates are equal" otherwise display "Dates are not equal".
#include <iostream>
using namespace std;
struct Date {
int day;
int month;
int year; };
int main() {
Date *ptr = new Date[2]; // Dynamically allocate memory for two Date objects
cout << "This program will ask you to enter two dates and then compare them." <<
endl;
cout << "---------------------------------------------------------------------------------" << endl;
for(int i = 0; i < 2; i++) {
cout << "Enter date " << (i + 1) << ":" << endl;
cout << "Enter the month (1-12): ";
cin >> ptr[i].month;
cout << "Enter the day (1-31): ";
cin >> ptr[i].day;
cout << "Enter the year (YYYY): ";
cin >> ptr[i].year;
// Compare the two dates
PF LAB 13 NUCES, ISLAMABAD Page 4 of 19
Structures LAB 13
if (ptr[0].day == ptr[1].day && ptr[0].month == ptr[1].month && ptr[0].year == ptr[1].year)
{
cout << "\nDates are equal." << endl;
} else {
cout << "\nDates are not equal." << endl;
// Free the allocated memory
delete[] ptr;
return 0;
2. Arrays of Structures
Just like we can declare an array of integers, similarly we can declare an array of
structures as well with the same syntax. To declare an array of structures, you must
first define a structure and then declare an array variable of that type.
To declare a 100-element array of structures of type emp, we will write:
emp employees [100];
This creates 100 elements arrays, and each element is a struct variable. To access a
specific structure, just use the index number with the array name. For instance, to
print the name of structure 8, write:
cout << employees[7].name ;
PF LAB 13 NUCES, ISLAMABAD Page 5 of 19
Structures LAB 13
Task03: Run the following example, and see its output.
#include <iostream>
#include<string>
using namespace std;
struct person { // Defining a structure
int age;
int salary;
};
int main() {
person p1[5]; // Declaring a struct variable
int i = 0;
char a[10];
for (int i = 0; i < 5; i++)
{
cout << "Enter age: ";
cin >> p1[i].age;
cout << "Enter salary: ";
cin >> p1[i].salary;
}
cout << "\nDisplaying Information.\n" << endl;
It Takes for
the (int
inputi and
= 0;using
i < 5;structures
i++) Arrays it take input of the age
and salary
{ of 5 peoples and then display the output again by using
structures Arrays
coutand
<< for loop
"Age: " << p1[i].age << endl;
cout << "Salary: " << p1[i].salary<<endl;
}
return 0;
}
PF LAB 13 NUCES, ISLAMABAD Page 6 of 19
Structures LAB 13
Task04: See the attached file Students.txt. You need to store that data in a structure
array. For this make a structure, and then declare a structure array. Once you have read
the file, now take input a roll number from the user, and then search that in the array. If
found display complete details of that student, else prompt that this student is not
registered.
#include <iostream>
#include <fstream>
using namespace std;
struct Student {
string name;
string cnic;
PF LAB 13 NUCES, ISLAMABAD Page 7 of 19
Structures LAB 13
string rollno;
float cgpa;
string program;
string phone; };
int main() {
const int MAX = 1000;
Student students[MAX];
int count = 0;
ifstream infile("Student.txt");
if (!infile) {
cout << "Unable to open file Student.txt" << endl;
return 1; }
string headerLine;
getline(infile, headerLine);
while (infile >> students[count].name
>> students[count].cnic
>> students[count].rollno
>> students[count].cgpa
>> students[count].program
>> students[count].phone) {
count++;
if (count >= MAX)
break;
}
infile.close();
string searchRollNo;
cout << "Enter Roll Number to search: ";
cin >> searchRollNo;
bool found = false;
for (int i = 0; i < count; i++) {
if (students[i].rollno == searchRollNo) {
cout << "\nStudent found!" << endl;
cout << "Name: " << students[i].name << endl;
cout << "CNIC: " << students[i].cnic << endl;
cout << "Roll No: " << students[i].rollno << endl;
cout << "CGPA: " << students[i].cgpa << endl;
cout << "Program: " << students[i].program << endl;
cout << "Phone: " << students[i].phone << endl;
found = true;
break;
}
}
PF LAB 13 NUCES, ISLAMABAD Page 8 of 19
Structures LAB 13
if (!found) {
cout << "\nThis student is not registered." << endl;
}
return 0;
}
Nested Structure
A structure can be nested inside another structure. The following code fragment illustrates
it:
struct addr
{
int houseno;
string area;
string city;
string prov;
};
struct emp
{
int id ;
string name;
addr address;
float salary;
};
Accessing Nested Structure Member
The members of structures are accessed using the dot operator. To access the "address"
member of the "emp" structure, we shall write:
emp e;
e.address.houseno
PF LAB 13 NUCES, ISLAMABAD Page 9 of 19
Structures LAB 13
Task05: Write a program to input information for five employees. Use structures as
defined above.
#include <iostream>
#include <string>
using namespace std;
struct addr { // given in the question
int houseno;
string area;
string city;
string prov;
};
struct emp { // given in the question
int id;
string name;
addr address;
float salary;
};
int main() {
const int SIZE = 5;
emp employees[SIZE];
for (int i = 0; i < SIZE; i++) {
cout << "Enter details for employee " << i + 1 << ":\n";
cout << "ID: ";
cin >> employees[i].id;
cin.ignore();
cout << "Name: ";
getline(cin, employees[i].name);
cout << "House No: ";
cin >> employees[i].address.houseno;
cin.ignore();
cout << "Area: ";
getline(cin, employees[i].address.area);
cout << "City: ";
getline(cin, employees[i].address.city);
PF LAB 13 NUCES, ISLAMABAD Page 10 of
19
Structures LAB 13
cout << "Province: ";
getline(cin, employees[i].address.prov);
cout << "Salary: ";
cin >> employees[i].salary;
cin.ignore();
cout << endl;
}
cout << "\n--- Employee Details ---\n";
for (int i = 0; i < SIZE; i++) {
cout << "Employee " << i + 1 << ":\n";
cout << "ID: " << employees[i].id << "\n";
cout << "Name: " << employees[i].name << "\n";
cout << "Address: House No. " << employees[i].address.houseno
<< ", " << employees[i].address.area
<< ", " << employees[i].address.city
<< ", " << employees[i].address.prov << "\n";
cout << "Salary: " << employees[i].salary << "\n\n";
}
return 0;
}
PF LAB 13 NUCES, ISLAMABAD Page 11 of
19
Structures LAB 13
PF LAB 13 NUCES, ISLAMABAD Page 12 of
19
Structures LAB 13
PF LAB 13 NUCES, ISLAMABAD Page 13 of
19
Structures LAB 13
Task06:
Write a C++ program for maintaining the library record of 10 books. Each record is
composed of 6 fields which include book’s library number, book’s name, author’s name,
edition number, year of publishing and price of the book. Choose appropriate data types to
represent each field. Your program should prompt the user to populate 10 records of this
database with some values and then print these values on screen.
Enter values for book 1 :
Book’s Name : A Guide to C Language Programming
Author’s Name : XYZ
Edition Number : 1 Year
of Publishing : 2013
Price of Book :
250
#include <iostream>
#include <string>
using namespace std;
struct Book {
int libraryNumber;
string bookName;
string authorName;
string editionNumber;
int yearOfPublishing;
float price;
};
int main() {
const int SIZE = 10;
Book books[SIZE];
for (int i = 0; i < SIZE; i++) {
cout << "Enter values for book " << i + 1 <<
":\n";
cout << "Library Number: ";
cin >> books[i].libraryNumber;
cin.ignore();
cout << "Book's Name: ";
getline(cin, books[i].bookName);
cout << "Author's Name: ";
getline(cin, books[i].authorName);
cout << "Edition Number: ";
cin >> books[i].editionNumber;
PF LAB 13 NUCES, ISLAMABAD Page 14 of
19
Structures LAB 13
cout << "Year of Publishing: ";
cin >> books[i].yearOfPublishing;
cout << "Price of Book: ";
cin >> books[i].price;
cin.ignore();
cout << endl;
}
cout << "\n--- Library Record ---\n";
for (int i = 0; i < SIZE; i++) {
cout << "Book " << i + 1 << ":\n";
cout << "Library Number: " <<
books[i].libraryNumber << "\n";
cout << "Book's Name: " << books[i].bookName <<
"\n";
cout << "Author's Name: " <<
books[i].authorName << "\n";
cout << "Edition Number: " <<
books[i].editionNumber << "\n";
cout << "Year of Publishing: " <<
books[i].yearOfPublishing << "\n";
cout << "Price of Book: " << books[i].price <<
"\n\n";
}
return 0;
}
PF LAB 13 NUCES, ISLAMABAD Page 15 of
19
Structures LAB 13
PF LAB 13 NUCES, ISLAMABAD Page 16 of
19
Structures LAB 13
PF LAB 13 NUCES, ISLAMABAD Page 17 of
19
Structures LAB 13
PF LAB 13 NUCES, ISLAMABAD Page 18 of
18