Question #44027

Write the main function that creates array of Product to store 5 objects of Product. Read
the cost and profit rate for each product from the user. For each product, display the cost,
the profit (using function profit defined in struct Product) and the selling price (cost +
profit) on screen.

Expert's answer

`#include <iostream>`

typedef struct {

double cost;

double profitRate;

double CalculateProfit () {

return cost* profitRate;

}

double CalculateSellingPrice() {

return CalculateProfit() + cost;

}

} Product;

int main() {

const int SIZE = 5;

Product* prod = new Product[SIZE];

std::cout <<"Enter cost and profit rate for 5 products.Profit rate is always smaller than 1." << std::endl;

for ( int i = 0; i < SIZE; i++ ) {

std::cout << "Enter data for product number " << i + 1 << std::endl;

std::cin >> prod[i].cost;

std::cin >> prod[i].profitRate;

}

for ( int i = 0; i < SIZE; i++ ) {

std::cout << "Product number " << i + 1 << std::endl;

std::cout << "Cost: " << prod[i].cost << "\n" << "Profit: " << prod[i].CalculateProfit() << "\n" << "Selling price: " << prod[i].CalculateSellingPrice() << std::endl << std::endl;

}

}

## Comments

## Leave a comment