Write a program in C++ to prepare product information using arrays.
Algorithm to solve the above problem:
1. Start
2. Create a class product.
3. Read the product name and cost.
4. Calculate the sum
5. Display the product information.
6. Stop the program.
Implementation of the above problem in C++:
#include<iostream>
using namespace std;
class product
{
int pro_code[50];
float pro_price[50];
int count;
public:
void cnt()
{
count=0;
}
void getproduct();
void displaysum();
void displayproducts();
};
void product::getproduct()
{
cout<<"Enter Product Code: ";
cin>>pro_code[count];
cout<<"Enter Product Cost: ";
cin>>pro_price[count];
count++;
}
void product::displaysum()
{
float sum=0;
for(int i=0;i<count;i++)
{
sum=sum+pro_price[i];
}
cout<<"Total Value:"<<sum<<"\n";
}
void product::displayproducts()
{
cout<<"\n Code Price\n";
for(int i=0;i<count;i++)
{
cout<<"\n"<<pro_code[i];
cout<<" "<<pro_price[i];
}
cout<<"\n";
}
int main()
{
product p;
p.cnt();
int x;
do
{
cout<<"\n1. Add a product";
cout<<"\n2. Display the product total value";
cout<<"\n3. Display all products";
cout<<"\n4. Quit ";
cout<<"\n Enter your choice: ";
cin>>x;
switch(x)
{
case 1: p.getproduct();break;
case 2: p.displaysum();break;
case 3: p.displayproducts(); break;
case 4: exit(0);
default: cout<<"\n Invalid choice";
}
}
while(x!=5);
return 0;
}
The output of the above program is tested on www.jdoodle.com
Output:
1. Add a product
2. Display the product total value
3. Display all products
4. Quit
Enter choice: 1
Enter Product Code : 10
Enter Product Cost : 100
1. Add a product
2. Display the product total value
3. Display all products
4. Quit
Enter choice: 1
Enter Product Code : 11
Enter Product Cost : 200
1. Add a product
2. Display the product total value
3. Display all products
4. Quit
Enter choice: 3
Code Price
10 100
11 200
1. Add a product
2. Display the product total value
3. Display all products
4. Quit
Enter choice: 4
Thanks
Mukesh Rajput
Thanks
ReplyDelete