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.
#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;
}
Comments
Leave a comment