Note - Double Click to Copy Code Contact Us!

Garage Management System using C++

Tech Doubility
Garage Management System using C++



 
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Car
  6. {
  7. private:
  8. string colour;
  9. public:
  10. Car(string* color_in);
  11. string getColor();
  12. };
  13. Car::Car(string* color_in)
  14. {
  15. colour= *color_in;
  16. }
  17. string Car::getColor()
  18. {
  19. return colour;
  20. }
  21.  
  22. using namespace std;
  23. int main()
  24. {
  25. //declare and initialize an array of garages
  26. Car* in_garages[10];
  27. //set all pointers to NULL
  28. for (int i = 0; i < 10; i++)
  29. {
  30. in_garages[i] = NULL;
  31. }
  32.  
  33. //temporary storages for keyboard input
  34. int choice = 0, garage_number;
  35. string temp_color;
  36.  
  37. while (choice != 3)
  38. {
  39. cout << "Enter 1 to put car in garage, 2 to remove it, 3 to quit.";
  40. cin >> choice;
  41. if (choice == 1)
  42. {
  43. cout << "Following garages are empty.";
  44. for (int i = 0; i<10; i++)
  45. {
  46. if(in_garages[i] == NULL)
  47. {
  48. cout << (i+1) << " " ;
  49. }
  50. }
  51. cout << endl << "Enter garage number for car to occupy ";
  52. cin >> garage_number;
  53.  
  54. //if user has an empty garage
  55. if (in_garages[garage_number-1] == NULL)
  56. {
  57. cout << "Enter color of car ";
  58. cin >> temp_color;
  59. //instance a new car object via pointer
  60. in_garages[garage_number-1] = new Car(&temp_color);
  61. cout << "Car of color " << in_garages[garage_number-1]->getColor() << " is in garage." << endl;
  62. }
  63. else
  64. {
  65. cout << "Garage is not empty." << endl;
  66. }
  67. }
  68. if (choice == 2)
  69. {
  70. cout << "The following garage numbers are occupied ";
  71. for (int i = 0; i < 10; i++)
  72. {
  73. if (in_garages[i] != NULL)
  74. {
  75.  
  76. cout << (i+1) << " ";
  77. }
  78. }
  79. cout << endl << "Enter garage number to empty.";
  80. cin >> garage_number;
  81. if (in_garages[garage_number-1] != NULL)
  82. {
  83. cout << "car of color " << in_garages[garage_number-1]->getColor()
  84. << " removed from garage." << endl;
  85.  
  86. delete in_garages[garage_number-1];
  87. in_garages[garage_number-1] = NULL;
  88. }
  89. else
  90. {
  91. cout << "Garage is already empty." << endl;
  92. }
  93. }
  94. }
  95. //cleanup not necessary
  96. return 0;
  97. }

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.