by Dinesh Thakur Category: Array Pointer and Union

When an element of a structure has to be accessed through pointer, then the -> operator is used. It is essential to initialize the pointer before it accesses any structure element.

 

node *loc;

start = new node;

start->bal = 15;

 

Example

 

The program inputs the details of products namely Product number, Product name, Cost price and selling price. It also displays the details of the products.

 

#include <iostream.h>

const int size=20;

struct r1

{

int Prodno;

char Prodname[size];

float Cprice;

float Sprice;

};

void main( )

{

r1 p1;

int I;

for(I=1;I<=3;I++)

{

cout<<" enter the product number ";

cin>>p1.Prodno;

cout<<" enter the product name ";

cin>>p1.Prodname;

cout<<" enter the cost price ";

cin>>p1.Cprice;

cout<<" enter the selling price ";

cout<<"\n";

cin>>p1.Sprice;

cout<<"Product Number "<<p1.Prodno<<"\n";

cout<<"------------------------";

cout<<"\n" ;

cout<<"Name of the product "<<p1.Prodname<<"\n";

cout<<"Cost Price "<<p1.Cprice<<"\n";

cout<<"Selling Price "<<p1.Sprice<<"\n";

}

}

 

Input and Output:

 

enter the product number 100

enter the product name Pentium

enter the cost price 50000

enter the selling price 55000

Product Number 100

-----------------------------

Name of the product Pentium

Cost Price 50000

Selling Price 55000

enter the product number 200

enter the product name Laser Printer

enter the cost price 28000

enter the selling price 30000

Product Number 200

-----------------------------

Name of the product Laser Printer

Cost Price 28000

Selling Price 30000

enter the product number 300

enter the product name Mouse

enter the cost price 2500

enter the selling price 2580

Product Number 300

-----------------------------

Name of the product Mouse

Cost Price 2500

Selling Price 2580



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles