Create a C++ program that reads 5 items and their prices and quantities. The program's basic requirements are as follows.
1.Declare four arrays of five elements each: item, price, quantity, and amount.
2. Declare the functions computeAmount, computeTotalAmount, displayItems, and strNew.
3.The expected output is depicted in the figure below.
strNew code:
//function that will adjust the string into specified length by adding spaces
string strNew(string str, int n) {
int l;
l = n - str.length();
for (int i = 1; i <= l; i++) {
str = str + " ";
}
return str;
}//end of strNew function
Output Example
Enter Item 1: Milk
Enter the price: 125.75
Enter the quantity: 3
Enter Item 2:
Enter Item 3:
Enter Item 4:
Enter Item 5:
Amount: 377.25
Total Amount: 377.25
=========================
Item Qty Price Amount
Milk 3 125.75 377.25
Cake 2 500 1000
Spaghetti 1 700 700
Fruits 10 50 500
Waffle 7 45 315
#include<iostream>
#include<string>
using namespace std;
double computeAmount(double prc, int qnt)
{
return prc*qnt;
}
double computeTotalAmount(double amount[],int N)
{
double res=0;
for(int i=0;i<N;i++)
{
res+=amount[i];
}
return res;
}
string strNew(string str, int n)
{
int l;
l=n-str.length();
for(int i=1;i<=l;i++)
{
str=str+" ";
}
return str;
}
void displayItems(string item[], double price[], int quantity[],double amount[],int N)
{
cout<<"\nItem Qty Price Amount\n";
for(int i=0;i<N;i++)
{
cout<<strNew(item[i],8)<<quantity[i]<<" "
<<price[i]<<" "<<amount[i]<<endl;
}
}
int main()
{
const int N=2;
string item[N];
double price[N];
int quantity[N];
double amount[N];
for(int i=0;i<N;i++)
{
cout<<"Enter item "<<i+1<<": ";
cin>>item[i];
cout<<"Enter the price: ";
cin>>price[i];
cout<<"Enter the quantity: ";
cin>>quantity[i];
amount[i]=computeAmount(price[i],quantity[i]);
cout<<"Amount: "<<amount[i]<<endl;
}
cout<<"\nTotal amount: "<<computeTotalAmount(amount,N)<<endl;
displayItems(item,price,quantity,amount,N);
}
Comments
Leave a comment