Hi

I need help on this please.I have to write a void function that reads data from the following file:5 2 2 670.60
6 4 2 890.80
2 2 0 220.00
10 8 1 1340.60
10 4 3 1430.70
4 0 0 460.30
5 3 1 700.00
7 5 2 1100.80
3 1 0 340.80.
and then do some math on it,but I am struggling to get the function to read the data line by line,it is reading only the last line.

Please if you can help,I will appreciate it!

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



void calculate(ifstream& in_stream, ofstream& out_stream) 
{
    ifstream inout;

    int numMembers, ordered, wine, totalOrdered, totalMembers;
    float billAmount, totalCommission, average, totalBillAmount;

    while(in_stream >> billAmount)
    {
        totalMembers += numMembers;   
        totalBillAmount += billAmount;
        average = totalBillAmount / totalMembers;
        if((numMembers >= 5) && (ordered > 4) && (wine > 2))
        {
        totalOrdered++;

        }
    }

    out_stream << "Number of families that ordered the special: " << totalOrdered << endl;
    out_stream << "Commission earned from the special meal: R" << billAmount << endl;
    out_stream << "Average spent per person for the evening: R" << average << endl;
}


int main( )

{

    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("orders.dat");
    out_stream.open("result.dat");

    calculate(in_stream, out_stream);

    out_stream.close();
    in_stream.close();

    return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

the data in the input file is in the following order:
number of members;number of family members that ordered a certain special; bottles of wine that they order and there bill amount.

My task is:
A restaurant has a special discount for families of at least 5 members.To get the discount,at least 4 members of the family must order the special and the family must order at least 2 bottles of wine.If the family order qualifies for the discount,the waiter gets an extra 3% commission on the bill amount.I must write a program that will read the file orders.dat and calculate the number of families that ordered the special,the commission earned by the waiter as well as the average spent per person(including all the people that he served for the evening.

Well, right now, you're only assigning the input to billAmmount. As it stands, totalMembers will always be equal to zero. 0 + 0 = 0. If you mean to take input for each variable then apply those to the total variables you may want to change your loop to look for the eof() of your input and simply take in input to each variable in the order in which they occur in the file.

while (!in_stream.eof()) //eof stands for "End of File".
{
 //inputs for each variable
 
 //mathematical assignments
}

Oh, and one last thing, what is:

ifstream inout;

for?

Edit(again): You do know you're billAmmount is the input and your totalBillAmmount is the sum of all the bills, right? You're only printing the BillAmmount.

Edited 5 Years Ago by Red Goose: n/a

No I removed it is not supposed to be there,I just out it there to try.

I used your code thanx but look what I have now,my program is still not working.What am I doing wrong.

while(!in_stream.eof())
    {
        totalMembers += numMembers;
        totalBillAmount += billAmount;
        average = totalBillAmount / totalMembers;
        if((numMembers > 5) && (ordered > 4) && (wine > 2))
        {
        totalOrdered++;
        totalCommission = (billAmount / 3 * 100);
        }
    }

No I removed it is not supposed to be there,I just out it there to try.

I used your code thanx but look what I have now,my program is still not working.What am I doing wrong.

while(!in_stream.eof())
    {
        totalMembers += numMembers;
        totalBillAmount += billAmount;
        average = totalBillAmount / totalMembers;
        if((numMembers > 5) && (ordered > 4) && (wine > 2))
        {
        totalOrdered++;
        totalCommission = (billAmount / 3 * 100);
        }
    }

You're not taking any input from your file.

How do I take input from the file,according to my prescribed book it should be:

while(in_stream >> numMembers >> ordered >> wine >> billAmount)

but it is not working.

How do I take input from the file,according to my prescribed book it should be:

while(in_stream >> numMembers >> ordered >> wine >> billAmount)

but it is not working.

How do you take input with "cin"? It's the same concept. They're both stream.

No I tried everything,I can't get it.

What, specifically, don't you get? How to take input? You take input the same you would with "cin".

while(in_stream.eof())
    {   
        [B]in_stream >> numMembers >> ordered >> wine >> billAmount;[/B]
        totalMembers += numMembers;
        totalBillAmount += billAmount;
        average = totalBillAmount / totalMembers;
        
        if((numMembers > 5) && (ordered > 4) && (wine > 2))
        {
        totalOrdered++;
        totalCommission = (billAmount / 3 * 100);
        }
    }

Like this,or what,Yes I cant seem to take input.

EDIT: Well, are your files in the right place? Your input file should be in the same directory as the "exe".

Edited 5 Years Ago by Red Goose: n/a

This article has been dead for over six months. Start a new discussion instead.