Answer to Question #318720 in C++ for Sheek

Question #318720

Write a program that prompts the user to enter the number of items and each item’s


name and price, and finally displays the name and price of the item with the lowest


price and the item with the second-lowest price.

1
Expert's answer
2022-03-26T15:29:37-0400
#include <iostream>
#include <string>

using namespace std;


struct Item
{
	string name;
	double price;
};

int main()
{
	int num;
	cout << "How many items do you want to enter? ";
	cout << "\nYour select: ";
	cin >> num;
	Item* arr = new Item[num];
	for (int i = 0; i < num; i++)
	{
		cout << "Please, enter the name of item " << i + 1<<": ";
		cin >> arr[i].name;
		cout << "Please, enter the price of item " << i + 1 << ": ";
		cin >> arr[i].price;
	}

	double lowest = arr[0].price;
	int count=0;
	for (int i = 0; i < num; i++)
	{
		if (lowest > arr[i].price)
		{
			lowest = arr[i].price;
			count = i;
		}
	}
	cout << "\nThe lowest price is " << lowest << endl;
	cout << "\nThe name is " << arr[count].name;

	double seclowest = arr[0].price;
	count = 0;
	for (int i = 0; i < num; i++)
	{
		if (seclowest > arr[i].price&&arr[i].price!=lowest)
		{
			seclowest = arr[i].price;
			count = i;
		}
	}
	cout << "\nThe second lowest price is " << seclowest<<endl;
	cout << "\nThe name is " << arr[count].name;
	delete []arr;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog