fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class car{
  7. public:
  8. int numWheels;
  9. int numWindows;
  10. double MPG; // km / l? UK???
  11. char name[80];
  12. };
  13.  
  14. int main(){
  15. int x;
  16. cout << "\nHow many cars would you like to add? ";
  17. cin >>x;
  18. cin.ignore(); //Need cin.ignore to ignore us hitting enter before we use cin.getline
  19.  
  20. vector<car> carVec;
  21. vector<car>::iterator carIter;
  22. car tempcar;
  23.  
  24. for(int i=0; i<x; i++){
  25. cout <<"\nName of car: ";
  26. cin.getline(tempcar.name, 79);
  27.  
  28. cout <<"\nNumber of windows: ";
  29. cin >> tempcar.numWindows;
  30.  
  31. cout <<"\nNumber of Wheels: ";
  32. cin >> tempcar.numWheels;
  33.  
  34. cout <<"\nMPG: ";
  35. cin >> tempcar.MPG;
  36. cin.ignore();
  37.  
  38. carVec.push_back(tempcar);//Put tempcar into the vector
  39. }
  40.  
  41.  
  42. for(carIter = carVec.begin(); carIter != carVec.end(); carIter++){ //Start at the beginning, iterate through until carIter = carvec.end
  43. cout << "Car #: " << (carIter - carVec.begin())+1 <<endl;// carIter - carVec.begin()+1 should equal to 1, or another number if you don't start at 1
  44. cout << "Name of car: " << carIter ->name <<endl; // Note -> is how we dereference. In this case It's VERY similar to how we use a subscript w/ an array
  45. cout << "Number of windows: " << carIter ->numWindows <<endl;//Same as above
  46. cout << "Number of wheels: " << carIter ->numWheels <<endl;//Same as above
  47. cout << "MPG: " << carIter ->MPG<<endl<<endl<<endl;//Same as above
  48. }
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 2864KB
stdin
2
Porsche carrera
2
4
15
Dodge Stratus
4
4
25.75
stdout
How many cars would you like to add? 
Name of car: 
Number of windows: 
Number of Wheels: 
MPG: 
Name of car: 
Number of windows: 
Number of Wheels: 
MPG: Car #: 1
Name of car: Porsche carrera
Number of windows: 2
Number of wheels: 4
MPG: 15


Car #: 2
Name of car: Dodge Stratus
Number of windows: 4
Number of wheels: 4
MPG: 25.75