Sitemap

Software Development-9

2 min readMay 5, 2024

--

C++ Structures/Struct

Structures, abbreviated struct, is the structure we need to learn before entering object-oriented programming.

For example; imagine a building, this is a structure. In this structure, we see it as a building with the processing of various interrelated materials and attributes such as concrete, columns, and iron. In the struct structure in coding, we can define the same multiple different variables and data types, that is, string bool, integer, float, and double data types, all in a struct. We can derive objects from this struct structure.

We have seen arrays-array- similar to this structure, but the difference is that we had to create a separate array for each variable type, whereas here each data type can be defined.

The reason why we use struct is because struct is a way to group various variables related to each other in one place. We will compare it with OOP in the next article, let’s just keep the following image in mind for now…

Press enter or click to view image in full size

1- way is to create a struct;

Press enter or click to view image in full size

Let’s improve the example above;

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

struct employees{

string staffName;

string department;

double salary;

int staffAge;

int staffid;

};

// Add a function to filter people with a salary equal to or less than a certain salary

//you can also do it as age!

void salaryFilter(employees persona[], int quantity, int salaryLimit)

{

cout<<”Salary”<<salaryLimit<<”or smaller:\n”;

for(int i=0; i < quantity;i++)

{

if(persona[i].salary <= salaryLimit)

{

cout<<”Staff Id:”<<persona[i].staffid<<endl;

cout<<”Personnel Name:”<<persona[i].staffName<<endl;

cout<<”Personnel Depaertman:”<<persona[i].department<<endl;

cout<<”Staff Salary:”<<persona[i].salary<<endl;

cout<<”Staff Age:”<<persona[i].staffAge;

Get Ceren Ağım’s stories in your inbox

Join Medium for free to get updates from this writer.

cout<<” — — — — — — — — — — — -\n”;

}

}

}

int main() {

srand(time(0)); //personnel id should be randomly determined in the given range …

int quantity;

cout<<”Number of Personnel Records(quantity):”;

cin>>quantity;

employees persona[quantity];

for(int i=0; i < quantity; i++)

{

cout<< i+1 << “. person — — -\n”;

persona[i].staffid = rand() % 8;

// set in random number range

// assigns a random number between 0 and 7

cout<<”Staff ID:”<<persona[i].staffid<<endl;

cout<<”Personnel Name:”;

cin>>persona[i].staffName;

cout<<” Personnel Department:”;

cin>>persona[i].department;

cout<<” Staff Salary:”;

cin>>persona[i].salary;

cout<<”Personnel Age:”;

cin>>persona[i].staffAge;

cout<<” — — — — — — — — — — — -\n”;

}

int salaryLimit;

cout<<” Enter the salary limit you want to filter:”;

cin>>salaryLimit;

//call the function here;

salaryFilter(persona,quantity,salaryLimit);

return 0;

}

Expected output:

Press enter or click to view image in full size

--

--

Ceren Ağım
Ceren Ağım

Written by Ceren Ağım

Coding and coding https://www.linkedin.com/in/ceren-959ba2155/ Ich fordere die Welt heraus... Behalten Sie die Straße im Auge...