Urgently need someone to help me with adding a procedure to print all of the car
ID: 3588580 • Letter: U
Question
Urgently need someone to help me with adding a procedure to print all of the cars driven by a given driver name. This procedure must accept a name parameter, being the name of the driver. For example, the procedure could be called to print all of the cars driven by "Charlotte". Also, change main to read in the name to check from the user, and then call the newly created procedure.
#include "splashkit.h"
#include <vector> //Include vector library
using namespace std;
/**
* Represents a Person within the program.
*
* @field name The name of the person
* @field age The person's age
*/
struct person
{
string name;
int age;
};
struct car //Declare car
{
string description;
person driver; //Declaring a variable within the struct,
//of type person, which is a struct itself
vector<person> passengers; //Dynamic array where each value is a person
//that array is called passengers,
// a dynamic array of person values
};
/**
* Read in a line of text from the user.
*
* @param prompt The message shown to prompt the user to input some text.
* @returns The line of text entered at the prompt.
*/
string read_string ( string prompt )
{
string result;
write(prompt);
result = read_line();
return result;
}
/**
* Read in a whole number (int) from the user. This will validate the user
* input and ensure that the value entered is a valid integer.
*
* @param prompt The message shown to prompt the user to input a number.
* @returns The integer value entered by the user.
*/
int read_integer ( string prompt )
{
string line;
int result;
line = read_string(prompt);
result = convert_to_integer(line);
return result;
}
/**
* Read in the data for a person. This will read in data for each field of
* the person, store it within a local variable, and then return this to
* the caller.
*/
person read_person()
{
person result;
result.name = read_string("Enter the person's name: ");
result.age = read_integer("Enter the person's age: ");
return result;
}
/**
* Print out the details of from person. This outputs relevant data about
* a Person to the terminal.
*
* @param to_print The person data to be printed.
*/
void print_person(const person &to_print)
{
write_line(to_print.name + " " + to_string(to_print.age));
}
car read_car() //Function to read in car value
{
int count; //Declare count variable
car result; //Create a variable (result)
result.description = read_string("Enter the car's description: ");
write_line("Provide the driver's details");
result.driver = read_person(); //Use read_person to read in all the data
//about a person, and store that driver
count = read_integer("How many passengers: "); //Read in how many passengers to add
for(int i = 0; i < count; i++)
{
person passenger; //Declare person variable
passenger = read_person(); //Read passenger data and store in variable
result.passengers.push_back(passenger); //Result is the car, inside the car we have
//the passengers (vector). Use the push_back
//method to add them to next slot in the list.
}
return result; //Car with all that data in it.
// A vector which contains a dynamic array of passengers
}
void print_car(const car &to_print) //Use the struct value as a parameter
{
write_line("---------");
write_line(to_print.description); //Details of the car
write_line("---------");
write_line("Driver: ");
print_person(to_print.driver); //Print out driver details
write_line("---------");
write_line("Passengers: ");
for(person p : to_print.passengers) //For each person, p, in, the passengers of the car
{
print_person(p);
}
write_line("----------");
}
void populate_cars(vector<car> &cars)
{
int count;
count = read_integer("How many cars do you want to add: ");
for (int i = 0; i < count; i++)
{
car temp;
temp = read_car();
cars.push_back(temp);
} //Reads car and pushes back onto vector
}
void print_all_cars(const vector<car> &cars)
{
for(car c : cars) //for each car, c, in the vector of cars
{
print_car(c); //print that car and
write_line(); //write a new line
}
}
int main()
{
vector<car> cars; //Array to add and remove elements to
populate_cars(cars); //Procedure that passes in cars by reference
print_all_cars(cars);
return 0;
}
Explanation / Answer
void search_cars_by_driver(const vector <car> cars, string driverName) {
write_line("---------");
write_line("Printing all the cars having driver as " + driverName);
for (car c: cars) //for each car, c, in the vector of cars
{
// if current car's driver name is expected one, print the car
if(c.driver.name == driverName) {
print_car(c); //print that car and
write_line(); //write a new line
}
}
}
int main() {
vector <car> cars; //Array to add and remove elements to
populate_cars(cars); //Procedure that passes in cars by reference
print_all_cars(cars);
string driverName = read_string("Enter the driver's name whom car need to be searched: ");
search_cars_by_driver(cars, driverName);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.