fork download
  1. // Array of objects
  2.  
  3. #include<iostream>
  4. //#include<conio.h>
  5. #include<string>
  6. using namespace std;
  7. #define SIZE 30
  8. class employee
  9. {
  10. char name[SIZE];
  11. float age;
  12. public:
  13. void getdata();
  14. void putdata();
  15. };
  16.  
  17. void employee::getdata()
  18. {
  19. cout << "Enter name:";
  20. cin.getline(name, sizeof(name));
  21. //EXCLUDED: cin.ignore();
  22.  
  23. cout << "Enter age:";
  24. cin >> age;
  25.  
  26. //INSERTED:
  27. // skip rest of line
  28. #if 0
  29. string dummy; getline(cin, dummy);
  30. #else
  31. cin.ignore();
  32. #endif // 0
  33. }
  34.  
  35. void employee::putdata()
  36. {
  37. cout << "Name is " << name << endl;
  38. cout << "Age is " << age << endl;
  39. }
  40.  
  41. const int size = 3;
  42.  
  43. int main()
  44. {
  45. employee manager[size];
  46. for(int i = 0; i < size; i++){
  47. cout << "\nThe details of the manager " << i+1 << endl;
  48. manager[i].getdata();
  49. }
  50. cout << endl;
  51.  
  52. for(int i = 0 ; i < size; i++){
  53. cout << "\nManager " << i+1 << endl;
  54. manager[i].putdata();
  55. }
  56.  
  57. //getch();
  58. return 0;
  59. }
Success #stdin #stdout 0s 4608KB
stdin
Michael
32
Jack
48
Joseph
53
stdout
The details of the manager 1
Enter name:Enter age:
The details of the manager 2
Enter name:Enter age:
The details of the manager 3
Enter name:Enter age:

Manager 1
Name is Michael
Age is 32

Manager 2
Name is Jack
Age is 48

Manager 3
Name is Joseph
Age is 53