TPS 11: Structs II
- Due Apr 14, 2021 by 10am
- Points 1
- Submitting a file upload
- File Types doc and docx
- Available after Apr 14, 2021 at 9am
Consider the following program and complete the three TODOs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//============================================================================== // File: tps11.cpp // Author: Hank Feild // Date: 14-Apr-2021 // Purpose: Demonstrates using vectors of structs. //============================================================================== #include <iostream> #include <vector> using namespace std; struct Car { string make, model; int year, miles; bool has4WheelDrive; }; /** * Reads in a car's information from the user. * * @return A Car populated with user input. */ Car ReadCarFromUser(){ Car car; string fourWheelDriveResponse; cout << "Please enter the following data about the car:" << endl; cout << "Make (Ford/Toyota/etc.): "; getline(cin, car.make); cout << "Model (F-150, Corolla, etc.): "; getline(cin, car.model); cout << "Year: "; cin >> car.year; cout << "Miles: "; cin >> car.miles; cout << "Does it have 4WD? [yes/no]: "; getline(cin, fourWheelDriveResponse); car.has4WheelDrive = fourWheelDriveResponse == "yes"; return car; } /** * Prints out a Car in a nice format: * * <year> <make> <model> with <miles> miles with<out?> 4-wheel drive * * @param car The Car to display. */ void PrintCar(Car car){ cout << car.year << " " << car.make << " " << car.model << " with " << car.miles << " miles with"; if(!car.has4WheelDrive){ cout << "out"; } cout << " 4-wheel drive" << endl; } /** * Asks the user for three cars, then prints them out. * * @return The exit status of the program; 0 is good. */ int main(){ // TODO 1: Declare a vector named `cars` that hold Car instances. // Read three cars in from the user. cars.push_back(ReadCarFromUser()); cars.push_back(ReadCarFromUser()); cars.push_back(ReadCarFromUser()); // TODO 2: add 10 miles to each car's mileage. // Print the cars. for(int i = 0; i < cars.size(); i++){ // TODO 3: Print the current car using the PrintCar function. } return 0; } |
After we've completed the group share portion, upload a copy of your updated TPS document.