| by Arround The Web | No comments

Array of Structs in C++

In C++, a data structure called an “array of structs” enables you to manage and store a group of connected data elements in a single variable. User-defined data types called “structures” allow you to combine the data members of various types into a single entity.

Working with collections of related data where each data item has numerous fields or characteristics is typical in many applications. You could need to keep the data on employees, for instance, where each employee has a name, age, income, and other details. In these circumstances, organizing the data via an array of structs can be both effective and practical.

Implementation of the Array of Structures in C++

Now, let’s understand how to declare the array of structures in the C++ programming language so that we can easily implement the array of structures during coding. To declare an array of structures in C++, we must first define the structure using the “struct” which is a predefined keyword. Then, list the structure’s name so that we can access the structure in the code and the brackets that enclose its fields. Declare the variables with their data type in the enclosed brackets. The datatypes of the variables could be different or the same.

Example 1: A Person Using Array of Structs in C++

Let’s start implementing the example of a person where we get the basic information of each person and display it on the user’s console window. For that, we first include the basic header files which are iostream and string header files, and use the using directive to bring the “std” namespace into the current scope. The iostream header file provides definitions for input and output streams in C++ such as cin and cout.

The string header file provides definitions for the string class, which is a convenient way to work with text in C++. The using directive allows us to use the names which are defined in the “std” namespace without having to qualify them with the “std::” prefix. This makes the code easier to read and write since we can simply write “cout” instead of “std::cout” every time we want to print something to the console.

Then, we create a structure called “Person” with three members: a name string, an age int, and a height double. This struct can be viewed as a user-defined data type that compiles related information into a single entity. Next, we define “people” as an array of Person structs with a size of 3. This indicates that we can store the data about up to three individuals in this array. A “Person” struct, which has members for name, age, and height, makes up each entry of the array.

#include <iostream>

#include <string>

using namespace std;

struct Person {

string name;

int age;

double height;

};

Person people[3];

int main() {

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

cout << "Enter name, age, and height for person " << i+1 << ": ";

cin >> people[i].name >> people[i].age >> people[i].height;

}

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

cout << endl;

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

cout << "Name: " << people[i].name << endl;

cout << "Age: " << people[i].age << endl;

cout << "Height: " << people[i].height << endl;

}

return 0;

}

Then, we start the main() function so that we can easily access the struct that we previously created. We use a for-loop in the main() method to ask the user to provide the data for three different users. The user is prompted to provide the name, age, and height of a person three times over the loop’s three iterations. Using “cin”, the input is read in before being allocated to the appropriate members of the people array’s Person struct. We enter the information for all three people and then display the details for each person using another for-loop.

The loop iterates three times. For each iteration, “cout” is used to display a person’s name, age, and height. In the end, we return 0 to the main() function. The following is the output of the previously-implemented program:

Example 2: Cars to Store the Information of Multiple Cars

Now, let’s look forward to the next example where we implement a car example where we store the information of cars and then display it. As what we did in the previous example, we always include the basic header files first which is important to write and run a program. In this code, we define a new C++ structure named “Car” that has four members: make, model, year, and price. Each member has its data type, with “make” and “model” being strings, the year being an int, and the price being double.

#include <iostream>

#include <string>

using namespace std;

struct Car {

string make;

string model;

int year;

double price;

};

int main() {

const int NUM_CARS = 3;

Car cars[NUM_CARS];

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

cout << "Car " << i+1 << " Information:" << endl;

cout << "Enter car make: ";

cin >> cars[i].make;

cout << "Enter car model: ";

cin >> cars[i].model;

cout << "Enter car year: ";

cin >> cars[i].year;

cout << "Enter car price: ";

cin >> cars[i].price;

}

double total_price = 0;

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

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

cout << "Make: " << cars[i].make << endl;

cout << "Model: " << cars[i].model << endl;

cout << "Year: " << cars[i].year << endl;

cout << "Price: $" << cars[i].price << endl;

cout << endl;

total_price += cars[i].price;

}

cout << "Total price of all cars: $" << total_price << endl;

return 0;

}

Next, we start the main() function. We create an array of “Car” structures called “cars”, with a size of NUM_CARS, which is defined as a constant with a value of 3 using the const keyword. This means that the car array can hold up to 3 car objects. The for-loop iterates through each element of the car array and prompts the user to enter an information about each car.

Inside the loop, we use the cout statement to display a message which asks the user to enter an information about the current car including its make, model, year, and price. We use the cin statement to read the user’s input for each of these fields and store it in the corresponding member variable of the current car object in the cars array. Specifically, cin reads in the user’s input as a string for the “make” and “model” fields and as an integer for the year field, and as a double for the price field. By the end of the loop, the cars array is filled with 3 car objects, each with information entered by the user.

Next, we calculate the total price of all the cars that were entered by the user. We first initialize a variable called “total_price” as a double with a value of 0. This variable is utilized to keep the total price of all the cars in the cars array. Inside the loop, it prints out information about each car to the console using cout. It prints out the make, model, year, and price of the current car object in the array. The “total_price” variable is also updated by adding the price of the current car object to the current value of the “total_price” using the += operator.

Once the loop is finished, the code prints out the total price of all the cars using cout to display the value of “total_price” to the console. The output is formatted to include a dollar sign in front of the total price. Here is the output:

Conclusion

We now learned what the array of structures is in C++ programming language. We discussed the implementation and declaration of the structure in the session. We implemented some examples to understand more about the concept of this topic.

Share Button

Source: linuxhint.com

Leave a Reply