newstrct.cpp 886 B

1234567891011121314151617181920212223242526
  1. // newstrct.cpp -- using new with a structure
  2. #include <iostream>
  3. struct inflatable // structure definition
  4. {
  5. char name[20];
  6. float volume;
  7. double price;
  8. };
  9. int main()
  10. {
  11. using namespace std;
  12. inflatable * ps = new inflatable; // allot memory for structure
  13. cout << "Enter name of inflatable item: ";
  14. cin.get(ps->name, 20); // method 1 for member access
  15. cout << "Enter volume in cubic feet: ";
  16. cin >> (*ps).volume; // method 2 for member access
  17. cout << "Enter price: $";
  18. cin >> ps->price;
  19. cout << "Name: " << (*ps).name << endl; // method 2
  20. cout << "Volume: " << ps->volume << " cubic feet\n"; // method 1
  21. cout << "Price: $" << ps->price << endl; // method 1
  22. delete ps; // free memory used by structure
  23. // cin.get();
  24. // cin.get();
  25. return 0;
  26. }