0% found this document useful (0 votes)
23 views6 pages

Lab 9

The document contains code for 3 tasks that demonstrate using structures in C++. Task 1 defines a book struct and array, then takes user input to populate the array. Task 2 defines a student struct, array, and function to display student data. It takes input and displays student details. Task 3 defines an employee struct, declares an instance, takes employee input, and displays the data. Each task shows defining, declaring, and using a struct to organize related data.

Uploaded by

AYESHA 21058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Lab 9

The document contains code for 3 tasks that demonstrate using structures in C++. Task 1 defines a book struct and array, then takes user input to populate the array. Task 2 defines a student struct, array, and function to display student data. It takes input and displays student details. Task 3 defines an employee struct, declares an instance, takes employee input, and displays the data. Each task shows defining, declaring, and using a struct to organize related data.

Uploaded by

AYESHA 21058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

LAB 9
TASK1:
#include<iostream>

using namespace std;

struct book_data {

int ISBN;

string book_name;

float book_pr;

};

int main() {

struct book_data book[5];

book[0].ISBN = 3456; // Add a semicolon here

book[0].book_name = "all is well";

book[0].book_pr = 1300;

for (int i = 0; i < 5; i++) {

cout << "Enter ISBN: ";

cin >> book[i].ISBN;

cout << "Enter name: ";

cin.ignore(); // Clear the newline character from the buffer

getline(cin, book[i].book_name);

cout << "Enter price: ";

cin >> book[i].book_pr;

Instructor: Mahrose Fatima Naqvi


PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

return 0;

TASK 2:
#include <iostream>

#include <string>

using namespace std;

// Define a structure to represent student data

struct Student_Data {

int Roll_No;

string Name;

float Marks;

};

// Function to display student data

void displayStudentData(const Student_Data& student) {

cout << "Roll No: " << student.Roll_No << endl;

Instructor: Mahrose Fatima Naqvi


PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

cout << "Name: " << student.Name << endl;

cout << "Marks: " << student.Marks << endl;

cout << endl;

int main() {

const int numStudents = 5;

Student_Data students[numStudents];

// Input data for 5 students

for (int i = 0; i < numStudents; i++) {

cout << "Enter Roll No for Student " << i + 1 << ": ";

cin >> students[i].Roll_No;

cin.ignore(); // Clear the newline character from the buffer

cout << "Enter Name for Student " << i + 1 << ": ";

getline(cin, students[i].Name);

cout << "Enter Marks for Student " << i + 1 << ": ";

cin >> students[i].Marks;

// Display the stored student data using the displayStudentData function

cout << "Student Details:" << endl;

for (int i = 0; i < numStudents; i++) {

cout << "Student " << i + 1 << ":" << endl;

displayStudentData(students[i]);

Instructor: Mahrose Fatima Naqvi


PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

return 0;

Task 3:
#include <iostream>

#include <string>

using namespace std;

// Define a structure to represent employee data

struct Employee {

int ID;

string Name;

double Salary;

Instructor: Mahrose Fatima Naqvi


PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

};

int main() {

// Declare an instance of the Employee structure

Employee emp;

// Input employee data

cout << "Enter Employee ID: ";

cin >> emp.ID;

cin.ignore(); // Clear the newline character from the buffer

cout << "Enter Employee Name: ";

getline(cin, emp.Name);

cout << "Enter Employee Salary: ";

cin >> emp.Salary;

// Display the stored employee data

cout << "\nEmployee Details:" << endl;

cout << "Employee ID: " << emp.ID << endl;

cout << "Employee Name: " << emp.Name << endl;

cout << "Employee Salary: " << emp.Salary << endl;

return 0;

Instructor: Mahrose Fatima Naqvi


PROGRAMMING FUNDAMENTALS (CSC131) Ayesha zaheer | pg#

Instructor: Mahrose Fatima Naqvi

You might also like