Introduction: Product Catalogue Analyser (C++ Coding)

This is simple product catalogue analyser, with some simple functions to analyse the costliest product, cheapest product and to organise the products based on the Owner Company ( because one Owner Company owns many products ). It is a C++ code that I have made, using simple concepts such as classes, friend functions and passing by references(pointers).

I got inspired to do this simple project after I got a similar question for my C++ Lab Exam.

One can go through the code and explanation, to understand what I have done and can easily make similar code with much more number of functions to do various other analyses. By including more data inputs and respective functions, one can make a full grown analysis program as per the his/her needs.

I hope you enjoy it.

PS : Basic knowledge on classes, friend functions and passing by references(pointers) is preferable.

Step 1: Having a Suitable IDE for Coding in C++

Basically, an Integrated Development Environment software is needed to type out a code and to compile and run it. It can be done without a complete IDE also but that is way too complex. I have a Microsoft Windows 8.1 OS and thus I use a Windows compatible IDE called DEV C++.

One can also use the Visual Studio IDE made by Microsoft, but I have done my project on this because I have been using it since a long time.

Internet search regarding an IDE for any OS can do done to get the required software to start coding!

Step 2: Parts of the Code (Intro to My Code.)

I have made the code such that, the user will enter the following details about his product :

1) Product Owner Company

2) Product Name

3) Product Number

4) Product Price

Thus I have declared three character array variables to store the Owner Company, name, number details. And I have declared a unsigned ( because prices can not be negative ) long integer to store the price of the product.

All of this I have encased in a class called "Product". This class will have some member functions to analyse and work on that data.

Now later on I declare a array of objects of type Product, to store each product's details, in the "main()" function, which will be discussed later.

Here is a detailed explanation of what functions I have put in the class and what they actually do. Further steps will highlight the algorithm behind each function ( It is not some top class algorithm, just a simple logic though :P )

Function 1:

update() :

This function will update each product's (object) details. The details will be asked and hence entered by the user. Whatever the user enters will be stored in the data members that I have specifically declared to store each respective detail ( data members given above ).

Function 2:


This function will take all the objects as a pointer argument ( not exactly all the objects, but there is a technique in pointers where a pointer to the address of first object of the array of the objects in used to access all of the objects in the array. As I am now working on an array of objects, I will be using this technique. ) It will display all the products manufactured by each Owner Company, i.e, it will display all the products based on their Owner Company.

Function 3:


This function will also use the pointer method to work on all the array of objects, to find and display all the details of the costliest product(s) in the entered data.

Function 4:


This function will also use the pointer method to work on all the array of objects, to find and display all the details of the cheapest product(s) in the entered data.

Step 3: Function Algorithms ( Function 1 )

Alright. I will try my best to make you understand what kind of logic I have followed.

Function 1:

void update()

This function is called in the "main()" function to let user enter data about each product. So, depending on the number of products that the user wants to enter, this will be called those many times. It has simple I/O commands to get the data and store the data in the respective data members.

(See image for code)

Step 4: Function Algorithm (Function 2)

Function 2 :

friend void company_sort(product *p,int n)

Friend function concept is used here because it is the most easiest way to make one common function a part of all possible objects that are ever going to be made.

Now this function takes the address of the first object in the array of objects into the pointer variable p, and also takes number of objects declared into the integer variable n.

Now I made another 2D array to store all the Owner company names into it.

char unique_company[n][50];

Then I copy all the object's "product_company" data into it. (First image)

After this I eliminate all product Owner Company names that are already there in the array by replacing it with the character "0". i.e. suppose there are two entries in this new character array of the same name ( say "Cadbury" ), then I simply replace the word "Cadbury" which has occurred the second time with a character "0". So, essentially in the end, in this new character array there will only be unique Owner Company names and "0"s. This I did by taking the first entry of the new array and checking with every other entry in the array ( checking for duplicates and eliminating them ). (Second image)

Now, I take each and every non "0" name in the new character array and match them with every single product Owner Company names of each and every object. First I take the first non "0" name in the new array and start checking with Owner Company names. Whenever it matches, I make it print all the details of the product. Then I move to the next non "0" name in the array and so on. So, by the end of the process the console(screen) will have all product details displayed Owner Company wise. (Third Image)

This is the functioning of the this function. My explaining skills are not that good, so I really hope that the above explanation is okay enough :) .

Step 5: Function Algorithm (Function 3)

Function 3:

friend void most_costly(product *p,int n)

This function too takes the address of the first object in the array of objects into the pointer variable p, and also takes number of objects declared into the integer variable n.

Now, there is something I learnt from my teacher. Whenever I want to search for the maximum value in a array of values, I declare a variable, say "max1", and give it the lower value than any value that will ever be entered in the array whose maximum value I want to find. Here, price of any product cannot be zero, so I am giving the lowest value of "0". Now I run a loop comparing every object's ( product's ) price with this variable "max1" of value 0. If the value at comparison (here the product's price) is greater than "max1", then I store that value (price) in the "max1", and continue to next value of the array( next product's price ). So for the immediate next comparison, we are actually comparing the previous product's price stored in "max1" with the present product's price. If the present product's price is greater than the previous product's then I store the present product's price in "max1". And this process continues. So, in the end I will actually have the highest price value in all of the objects(products), stored in "max1". So, I found out the maximum price now!

After this I run another loop, comparing this value in "max1" (which is the maximum price) with every product's price, and whenever it matches, I print out the product's details, which in essence is printing out the costliest product's details!

(See image for the code)

PS : For any code part that I have missed putting pics of, please refer to the code files that I have uploaded. ( For ex : There is no photo showing the code for initializing the "max1" variable to 0. :P )

Step 6: Function Algorithm (Function 4)

Function 4:

friend void most_cheap(product *p,int n)

This is very similar to previous function, but this time I take a variable called "min1" having a value higher than any value that will ever be stored in the array under comparison. And whenever the product's price is lesser than "min1", I store its value in "min1" and this process continues. So, essentially in the end I have the least price of product stored in "min1". And the way to print the details of products with least price is similar to that of the previously explained function.

(See image for code)

Step 7: Final Part of Code (The User Interaction Part)

Now, with all the functions defined and guns loaded :P, I show in what way the user interacts with this.

All the following stuff happens in the "main()" function.

I first ask user to enter the number of products that he wants to enter the details of. Then I create those many Objects(products). And then I ask the user to input the details of each product. Once this is done, I give the user a set of numbers which when entered will do certain functions, like "Enter 1 to do this, 2 to do that......etc.....".

When the user enters his choice, the required action will be taken. Option will also be given for the user to quit the program if he is done with what he wants.

(See image for the code)

Step 8: Thank You!

So, this is the code that I have worked on. It is simple. And it has only three workable functions and hence does only three types of analyses. One can look at the way I have done things to add any number of functions to make the program more powerful with more functionalities.

For complete code please see the ".cpp" file attached. The same code is also in a ".txt" file ( in case you face problems with the ".cpp" file).

I hope this has helped you all in some or the other way. Do comment if you have any doubt or need some clarification. Criticism also is accepted :P . Thats all. Thank You!

Please do vote if you like it!

Coded Creations

Participated in the
Coded Creations