Write a C++ program which uses a structure type that stores data records for a c
ID: 3834075 • Letter: W
Question
Write a C++ program which uses a structure type that stores data records for a car, as described in the Requirements below. Requirements: Define a global structure and name it as Car. This structure has these member variables: o Make (string), model (string), year (int), mileage (double), original_price (double), and
sale_price(double) o sale_price will be calculated based on original_price and mileage. Declare two variables from structure car and call it car_number1 and car_number2. Write a function called get_input, which is responsible to o Get the input values for the member variables of car_number1 and car_number2 o For sale_price enter zero. This value will be updated later by function price_calculation o This function uses call by reference for car_number1 and car_number2 o For string member variables use the correct input method Write a function called price_calculation which gets car_number1 and car_number2 as input (call
by reference) and update the sale_price. o If the car mileage is greater than 70,000 miles then the sale_price is 70% of the
original_price. Otherwise the sale_price is 85% of the original_price.
Write a function called print_output which is responsible o To print the information for each car separately (including the update value of sale_price) o This function should print the output based on alphabet order of car’s make.
A sample run of your program for one item should look like below.
Enter the information for the first car Enter the car’s make: Toyota Enter the car’s model: Camry Enter the car’s year: 2000 Enter the car’s mileage: 110000 Enter the car’s original_price: 7000$ Enter the car’s sale_price:0;
Enter the information for the second car Enter the car’s make: Ford Enter the car’s model: Mustang Enter the car’s year: 2014 Enter the car’s mileage: 40000 Enter the car’s original_price: 15000$ Enter the car’s sale_price:0;
Check the car’s information including the sale_price (ordered alphabetically)
Ford Mustang
year: 2014
mileage: 40000
sale_price: 12,750$
Toyota Camry
year: 2000
mileage: 110000
sale_price: 4,900$
Explanation / Answer
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C/C++.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace std;
struct Car
{
char *make=new char(50);
char *model=new char(50);
int year=0;
double mileage=0;
double original_price=0;
double sale_price=0;
};
void get_input(Car *c)
{
cout << "Enter the car’s make:";
cin >> c->make;
cout << "Enter the car’s model:";
cin >> c->model;
int year;
double mileage,original_price,sale_price;
cout << "Enter the car’ s year:";
cin >> year;
c->year=year;
cout << "Enter the car’s mileage:";
cin >> mileage;
c->mileage=mileage;
cout << "Enter the car’s original_price:";
cin >> original_price;
c->original_price=original_price;
cout << "Enter the car’s sale_price:";
cin >> sale_price;
c->sale_price=sale_price;
}
void price_calculation(Car *c1)
{
if(c1->mileage > 70000)
{
c1->sale_price=(70*c1->original_price)/100;
}
else
{
c1->sale_price=(85*c1->original_price)/100;
}
}
void print_out(Car *c)
{
cout << c->make << " " << c->model << " ";
cout << "year: " << " " << c->year << " ";
cout << "Mileage: " << c->mileage << " ";
cout << "sale_price "<< c->sale_price << "$" << " ";
}
void print_output(Car *c1,Car *c2)
{
if(strcmp(c1->make, c2->make) < 0)
{
print_out(c1);
print_out(c2);
}
else
{
print_out(c2);
print_out(c1);
}
}
int main()
{
Car *car_number1=new Car;
Car *car_number2=new Car;
cout << "Enter the information for the first car";
get_input(car_number1);
cout << "Enter the information for the second car";
get_input(car_number2);
price_calculation(car_number1);
price_calculation(car_number2);
print_output(car_number1,car_number2);
return 0;
}
Output:
Enter the information for the first carEnter the car’s make:Toyoto
Enter the car’s model:Camry
Enter the car’ s year:2000
Enter the car’s mileage:110000
Enter the car’s original_price:7000
Enter the car’s sale_price:0
Enter the information for the second carEnter the car’s make:Ford
Enter the car’s model:Mustang
Enter the car’ s year:2014
Enter the car’s mileage:40000
Enter the car’s original_price:15000
Enter the car’s sale_price:0
Ford Mustang
year: 2014
Mileage: 40000
sale_price 12750$
Toyoto Camry
year: 2000
Mileage: 11000
sale_price 4950$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.