Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,961 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,543 people online right now. Registration is fast and FREE... Join Now!




[c++] Calculation from text file. Help.

 
Reply to this topicStart new topic

[c++] Calculation from text file. Help.

Ragnarok
22 Apr, 2007 - 08:05 PM
Post #1

New D.I.C Head
*

Joined: 22 Apr, 2007
Posts: 6


My Contributions
Hi, I have a function that read from a text file and calculate each line from text file. The calculation is the total profit made from each sale(each line from text file).
After compiling the code it doesn't give the result that I want, instead the same answer or result from each line(each sale). Please point me to the right direction. Thank you.

CODE

int profitCookie(){
    int sold;
    int calculate;
    int n;
    int count = 0;
    int total = 0;
    int make;

    ifstream Input;
    Input.open("data.txt");

    while(!Input.eof())
    {
    Input>>n;


        if (n<12){
            sold=25;
            make=10;
        }
        if (n>=12){
            sold=20;
            make=10;
        }
        if (n>=60){
            sold=25;
            make=10;
            n+5;
        }
        
        
        int calculate = (int (n) * int (sold)) - (int (n) * int (make));
    }

Input.close();
return calculate;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: [c++] Calculation From Text File. Help.
23 Apr, 2007 - 04:11 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,225



Thanked: 36 times
Dream Kudos: 25
My Contributions
Slight change in structure.
CODE


        if (n<12){
            sold=25;
            make=10;
        }
        else if ((n>=12) && (n<60)){
            sold=20;
            make=10;
        }
        else if (n>=60){
            sold=25;
            make=10;
            n+5;//what is this line supposed to do?
        }
else{
   cout<<"unexpected input\n";
}


User is online!Profile CardPM
+Quote Post

Ragnarok
RE: [c++] Calculation From Text File. Help.
23 Apr, 2007 - 12:17 PM
Post #3

New D.I.C Head
*

Joined: 22 Apr, 2007
Posts: 6


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Ragnarok
RE: [c++] Calculation From Text File. Help.
14 May, 2007 - 07:09 PM
Post #4

New D.I.C Head
*

Joined: 22 Apr, 2007
Posts: 6


My Contributions
I have done the code but it seem there are problem adding the total of each profit. Can anyone help me check my code. Thank you.
CODE

#include <iostream>
#include <fstream>
using namespace std;

//read data
int read_data(ifstream &);
int cookieAverage(ifstream &);


int read_data(ifstream &file) {
    int data;

    file >> data;

    return data;
}

//Each cookie profits and total profit
float profitCookie(){
    float sold=0;
    float extra=0;
    float total=0;
    float make=.10;

    ifstream Input;
    Input.open("data.txt");

    float calculate=0;
    int data=0;


    while(!Input.eof())
    {
    Input>>data;


    if (data<12 && data >0){
            sold=.25;
            calculate = (data * sold) - (data * make);
            cout<<data<<" "<<"$"<<calculate<<endl;

        }
        else if ((data>=12) && (data<60) && data>0){
            sold=.20;
            calculate = (data * sold) - (data * make);
            cout<<data<<" "<<"$"<<calculate<<endl;

        }
        else if (data>=60 && data>0){
            sold=.25;
            //n+5;//what is this line supposed to do?
            calculate = ((data * sold) - (data * make)) - .50;
            cout<<data<<" "<<"$"<<calculate<<endl;
        }


        total = total+calculate; //<---------this code that add the total of each profit


    }

        cout<<endl;


Input.close();
return total;
}

//Average & total
int cookieAverage(){
    int n;
    ifstream Input;

    Input.open("data.txt");


    int total=0;
    int count=0;

    while(!Input.eof())
    {
        Input>>n;
        total=total+n;
        count++;
    }


    double average;
    average=double(total)/double(count);
    cout<<"Total cookie sold is "<<total<<" and average cookie sold is "<<average;
Input.close();
return 0;
}


//Main Display
int main()
{

     cout<<"Order Detail"<<endl;

       ifstream data("data.txt");

    // While the file has not reached the end of the
    // file, read in the data.
    while(!data.eof())

       cout << read_data(data)<<endl;
       cout<<endl;
       cout<<"Profit for each order"<<endl;
       cout<<profitCookie()<<endl;
       cout<<endl;
       cout<<cookieAverage()<<endl;

data.close();
return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month