-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

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.

No comments:

Post a Comment