In: Computer Science
Program Assignment 1
C++ please
Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names. A second array will be used for the price of each bottle of BBQ sauce; this will also be initialized from a file. The two files can be different files. The third array will be used to store the amount sold of each type of sauce. The program will then prompt the user for how many of each sauce has been sold. Once all the data is entered a report is created that displays each of the sauce types with the total amount sold and the total sales for each of the sauces. This output should be in a table format. The program will also calculate the overall total of sales for all of the sauces. It will then determine which sauce was the highest selling and the lowest selling product.
Validations:
1.) The files can be opened
2.) No negative values for the amount of sales for each sauce. This should be a loop asking for the input again.
Requirements:
1. When prompting for the amount sold of a sauce, the name of the sauce must be used in the prompt.
2. Table output should have evenly spaced columns.
3. Where there is currency being displayed, it must be formatted with 2 decimal places and a $.
4. Method to display the table of output.
5. Method to calculate the total of overall sales, returns the amount.
6. Method to get the inputs from the user for the amount sold of each sauce.
7. Method to determine which is the highest and lowest product sold.
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//method to print the details in tabular format
void print(string name[],int sale[],double price[])
{
int i;
cout<<endl<<"==============================================================================";
cout<<endl<<"Names of Sauce\t\tTotal Amount
Sold\t\tTotal Sales";
cout<<endl<<"==============================================================================";
for(i=0;i<6;i++)
{
if(name[i].length()<6) //condition to
maintain the uniform space
cout<<endl<<name[i]<<"\t\t\t\t"<<sale[i]<<"\t\t\t$"<<fixed<<setprecision(2)<<sale[i]*price[i];
else
cout<<endl<<name[i]<<"\t\t\t"<<sale[i]<<"\t\t\t$"<<fixed<<setprecision(2)<<sale[i]*price[i];
}
}
//method to return the total number of sales done
int totalsale(int sale[])
{
int i,total=0;
for(i=0;i<6;i++)
total = total + sale[i]; //compute the total
sale
return total; //return the total sale
}
//method to input the amount of items sold
void amountsold(string name[],int sale[])
{
int i;
for(i=0;i<6;i++)
{
cout<<endl<<"Enter the amount of
sold for "<<name[i]<<" Sauce";
cin>>sale[i]; //input the sale
if(sale[i]<0) //validate the sale
i--;
}
}
//method to display the highest and lowest sale
void highlow(string name[],int sale[])
{
int i,mi,ma,min,max;
min=sale[0]; //assign the first value as min
sale
max=sale[0];//assign the first value as max
sale
for(i=0;i<6;i++)
{
if(min>sale[i]) //condition to
find the min sale
{
min=sale[i]; //assign min sale to
min
mi=i;//assign the
index to mi
}
if(max<sale[i])
{
max=sale[i]; //assign max sale to
min
ma=i;//assign the
index to ma
}
}
//display the result
cout<<endl<<name[mi]<<" Sold less
quantity";
cout<<endl<<name[ma]<<" Sold high
quantity";
}
//driver program
int main()
{
string name[6],str;
double price[6],p;
int sale[6],i,j;
//declare file objects
ifstream infile1,infile2;
//open the name file in read mode
infile1.open("D:/names.txt");
//if not open
if(!infile1)
{
cout<<endl<<"Unable to open
the source file";
exit(0);
}
//open the price file in read mode
infile2.open("D:/price.txt");
//if not open
if(!infile2)
{
cout<<endl<<"Unable to open
the source file";
exit(0);
}
//loop to read the names and store it name array
i=0;
while (getline(infile1, str)) {
name[i]=str;
i++;
}
//loop to read the price and store it in price array
i=0;
while(! infile2.eof())
{
infile2>>p;
price[i]=p;
i++;
}
//call to amountsold() to input the sale amount
amountsold(name,sale);
//call to print() to print in tabular format
print(name,sale,price);
//call to totalsale() to display the total sale
j=totalsale(sale);
//display the total sale
cout<<endl<<"Total sale : "<<j;
//method call to display the highest and lowest sale
highlow(name,sale);
}
OUTPUT
VALUE OF NAME AND PRICE FILE