C++ | Introduction to Structure

Introduction

This is a tutorial of structures in c++. The Program is given below that uses structure. The program is extendable. Go enjoy the program. Lets begin……

Program to find percentage of students using structure.

#include<iostream.h>
#include<conio.h>
struct stud
{
int rno,s1,s2,s3,tot,per;
char name[40];
};
struct stud s[3];
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//Input the necessary inputs
for(i=0;i<3;i++)
{
cout<<“Enter the rno :”;
cin>>s[i].rno;
cout<<“Enter the name :”;
cin>>s[i].name;
cout<<“Enter all marks :”;
cin>>s[i].s1>>s[i].s2>>s[i].s3;
//calculate the total and percentage
s[i].tot=s[i].s1+s[i].s2+s[i].s3;
s[i].per=s[i].tot/3;
}
//headings
cout<<“R.no\tName\tTotal\t%age”<<endl;
//show the output
for(i=0;i<3;i++)
{
cout<<s[i].rno<<“\t”<<s[i].name<<“\t”<<s[i].tot<<“\t”<<s[i].per<<endl;
}
//get character
getch();
}

Output

Enter the rno :1
Enter the name :abc
Enter all marks :90
90
90
 Enter the rno :2
Enter the name :efg
Enter all marks :100
100
100
 Enter the rno :1
Enter the name :hij
Enter all marks :100
90
80
Rno. Name Total %age
 1        abc      270    90
 2       efg       300   100
 3       hij       270    90

//sorry the above table is not printed here properly. But your c++ software will do.

How does it work

  1. You enter all inputs.
  2.  The total and percentage is calculated.
  3. The table is printed.

Extending it

The program can be extended by using the concept in any program. Like for ex. you can create a same program for employees in company and their salary. Go ahead extend it……

  • Remember this is a basic part of c++ which can be used in any program….

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare structure.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the variables as int (name them as you want.)
  6. Add the cout and cin of  all necessary inputs.
  7. Calculate the total and percentage.
  8. Print the table.

At the end

You learnt creating the C++ program for Introduction to structures in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s