-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;
Showing posts with label Structures. Show all posts
Showing posts with label Structures. Show all posts

Sunday, 7 June 2015

Using loops in structures


Hello and Asalam o Alikum. Today I am here with a new blog that will take demonstate the use of loops in structures.

In the program,I will take input from the user of his/her favourite movies and finally display the list of his/her favourite movies using structures in C++.I will use a loop to input 3 movies from the user.

So,below is the source code of this program.

Source code

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4. using namespace std;
  5. struct movies
  6. {
  7. string movie;
  8. };
  9. main()
  10. {
  11. movies m[3];
  12. for(int i=0;i<=2;i++)
  13. {
  14. cout<<"Enter the movie of your choice:";
  15. getline(cin,m[i].movie);
  16. }
  17. cout<<endl;
  18. cout<<"\t"<<"\t"<<"List of your favourite movies"<<"\t"<<"\t"<<endl<<endl;
  19. for(int i=0;i<=2;i++)
  20. {
  21. cout<<i+1<<"."<<m[i].movie<<endl<<endl;
  22. }
  23. }

Output


Explanation
I have used <string> header file in line # 3 because I have "string" data type in line # 7.

In line # 11, I have declared an array of size 3 and data type movies.

In line #12, a "for" loop is used for 3 iterations.

Remember that if we do not use the header file <string>, then we cannot use getline( ) function as used in line # 16.

Friday, 5 June 2015

Maintaining records in structures

As discussed earlier, structures are of great important for user-defined data types.Today I will discuss the use of functions in structures in C++ programming.

Below is the source code of a program in which I will maintain the record of two patients using structure in C++.I will use two functions in it;one for getting the record of the patients and one for displaying the record of the patients.So,before going into the further details of the program,lets discuss its source code first.

Source code

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4. using namespace std;

  5. struct patient
  6. {
  7. string name;
  8. string disease;
  9. };

  10. void getrecord(patient &p1 ,patient &p2)
  11. {
  12. cout<<"Enter the name of first patient:";
  13. cin>>p1.name;
  14. cout<<"Enter the name of disease:";
  15. cin>>p1.disease;
  16. cout<<"Enter the name of second patient:";
  17. cin>>p2.name;
  18. cout<<"Enter the name of disease:";
  19. cin>>p2.disease;
  20. }

  21. void displayrecord(patient p1, patient p2)
  22. {
  23. cout<<"Name of first patient:"<<p1.name<<endl;
  24. cout<<"Disease of first patient:"<<p1.disease<<endl;
  25. cout<<"Name of second patient:";
  26. cout<<p2.name<<endl;
  27. cout<<"Disease of second patient:";
  28. cout<<p2.disease<<endl;
  29. }
  30. int main()
  31. {
  32. patient p1,p2;
  33. char ch;
  34. int n;
  35. do 
  36. {
  37. cout<<"Enter 1 for getting record and 2 for displaying record:";
  38. cin>>n;
  39. switch(n)
  40. {
  41. case 1:
  42. getrecord(p1,p2);
  43. break;
  44. case 2:
  45. displayrecord(p1,p2);
  46. break;
  47. default:
  48. cout<<"Invalid choice!";
  49. }
  50. cout<<"Enter y to continue and n to terminate:";
  51. cin>>ch;
  52. }
  53. while(ch!=n);
  54. getch();
  55. return 0;
  56. }

Output


Explanation:

In this program,I have declared a structure named "struct".

In line # 13, I have declared a function for getting the record from the user in which I have passed the values by reference.

In line # 24, I have declared a function for displaying the data entered by the user.

In line # 42, I have declared a "switch" in which there are two cases;one case for getting the record from the user and one case for displaying the record from the user.

Monday, 1 June 2015

Simple example of structures

Feeling difficulty in understanding the concept of structures in C++?
Simply e mail me at "G.A.Mujtaba11@gmail.com".

Today I am here with a simple example of structures in C++.Before explaining the example,let me tell you a very important concept about structures,"member access operator/dot operator".

Dot operator/Member access operator

If you want to access a variable in structures,then you should use "dot operator".
For example,
struct structure
{
int data
};
As you see in the above example that a structure is declared that contains a variable named as "data".
Suppose if you want to access that variable in main( ) function,then you should use dot operator as given below

structure s1;  //////s1 has data type structure
s1.data;   ///////this "." is pointing to the variable being declared in the structure.

Hope you understand this concept.Now below is the simple example of structures in C++.

Source code

structures example in C++

Output



Remember that the data type of employ is "database" in the above example.I am not taking any input from the user in the above program.In my next blog, I will inshallah (if Allah Almighty wants) concentrate on little complex program in which I will take inputs from the user.

I will recommend you to practice this simple code which will help you to better you concepts of structures in C++.

Saturday, 30 May 2015

Concept of structures

Structures


There are many data types in C++.i.e.,int,float,double,long double,bool,etc.

We know that int is used to represent integers and float is used to represent numbers with precision.

But in case of real world objects(such as car,chair,door,etc),these data types are not useful.

So,to remove this difficulty, we use user-defined data types and these data types are known as structures.

Definition:

                                       A structure is a collection of variable types grouped together.You can refer to a structure as a single variable, and to its parts as members of that variable by using the dot (.) operator. 

Remember that the variables in a structure are of different data types.

When dealing with the students in a college, many variables of different types are needed.  It may be necessary to keep track of a name, an address (street, city, state, zip code), an age, an ID number, and a grade point average, for example,below is the general syntax of declaring a structure or user-defined data type in C++.


struct STUDENT_TYPE

{

string name;

string street; 

string city;

string state;

string  zipcode;  

 int age; 

 double IDnum;    

 double grade;

} ;


(reference taken from www.MathBits.com)