I think I should rewrote my code but I don't want to make new thread and post what I'm trying to do. Here it goes. Amadeus the n+5; it's the third if statement on the total profit, which add 5 cookies.
A chocolate chip cookie store has a program display the statistics on cookie sales.
Fact: Cookies cost 10 cents to make, but are sold for 25 cents.
Keep track of:
* Total number of cookies sold.
* Average number of cookies sold (rounded to the next highest integer).
* Total Profit
o If an order contains less than 12 cookies, the cost is 25 cents per cookies.
o If an order contains 12 or more cookies, the price is not 25 cents per cookie, but 20 cents per cookie. Make sure you calculate the profit correctly.
o If an order is for 60 cookies or more, give them 5 free cookies. These extra 5 cookies are not part of the profit for the order. In fact, they are part of a loss. So, subtract 5 cookie's from the profit of the order (10 cents per cookie profit loss). However keep track of the amount of cookies ordered for the total number ordered. Keep track separately of the free cookies.
* Free Cookies
Make functions used to:
* Read in the data from the file.
* Calculate the average number of cookies.
* Calculate the profit for an order.
* Display the statistics.
A data file called data.txt contains cookie order quantities, one on each line. A value of 0 is used to indicate the end of the data.
Read in the numbers from a file called data.txt, as long as a trailer value of 0 is not reached. Download the sample file to use for the project, so that way I'll have all the same values for each student.
For each number read in (except for 0) do the following:
* Add the number to the total cookie sales
* Calculate the profit for that order, and add it to the total profit.
After reading in a value of 0, display the sales statistics:
* Total cookies sold
* Average number of cookies sold (rounded up to the nearest integer)
* Profit
* Free cookies given away
After the statistics are displayed, exit the program.
CODE
#include <iostream>
#include <fstream>
using namespace std;
ifstream Input("data.txt");
void read_data(int arr[],int n) {
Input.open("data.txt");
for(int i=0;i<n;i++)
Input>>arr[i];
}
int cookieAverage(int arr[],int n ){
int sum;
for(int i=0;i<n;i++)
sum +=arr[i];
double average = sum/n;
cout<<"Total cookie sold is "<<sum<<" and average cookie sold is "<<average;
return 0;
}
//Each cookie profits and total profit
int profitCookie(int arr[],int n){
int sold;
int calculate;
int count = 0;
int total = 0;
int make;
ifstream Input;
Input.open("data.txt");
for(int i=0;i<n;i++)
{
if (arr[i]<12){
sold=25;
make=10;
}
else if (arr[i]>=12){
sold=20;
make=10;
}
else if (arr[i]>=60){
sold=25;
make=10;
arr[i]+5;
}
double calculate = (arr[i] * sold) - (arr[i] * make);
}
Input.close();
return calculate;
}
//Main Display
int main()
{
int n;
Input >> n;
int arr[n];
read_data(arr,n);
cout <<profitCookie(arr,n)<<endl;
cout<<endl;
cout<< cookieAverage(arr,n)<<endl;
Input.close();
return 0;
}
This post has been edited by Ragnarok: 23 Apr, 2007 - 12:24 PM