Monthly totals from a sales report file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<fstream>
#include <string>
#include <sstream>
#include<vector>

using namespace std;

void writeFile();
void readFile(ifstream &inputFile);

int main()
{
	writeFile();
	ifstream inputFile;
    
    inputFile.open("QuarterlySalesDataFile.txt");

    if (!inputFile) 
    {
        cout << "Unable to open file";
    }

    else 
    {

	readFile(inputFile);//arguments

	inputFile.close();
     }

return 0;
}

void writeFile()
{
	ofstream outputFile;
	string filename;
	cout<<"Enter your filename to save";
	cin>>filename;
	cout<<"File created!";
	outputFile.close();
}
void readFile(ifstream &inputFile)//parameters

{

int iD, targetId=0, i=0;

int monthI, month[12]={}, amntTotal=0;

float amountofSale, amnt=0, accountTotal=0;
float commissionRate=0.26;
float commission, comRate;

string months[12]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

bool firstiD=true;

while (inputFile>> iD>>monthI>>amountofSale)
{

accountTotal+=amountofSale;
commission=commissionRate*amnt;

    if(firstiD)	
{
    targetId=iD;
    firstiD = false;	
}
    if (targetId==iD)
    {
    cout <<endl<< iD << ' ' << months[monthI-1] << ' ' << amountofSale<<endl;
		
    amnt+=amountofSale;	
	
    }

    else 
    {
    cout<<amnt<<endl;
    comRate=commissionRate*accountTotal;
    cout<<commission<<endl;
    amnt=0;
    commission=0;
    amnt+=amountofSale;	
    cout << iD << ' ' << months[monthI-1] << ' ' << amountofSale<<endl;
    targetId=iD;
    firstiD=true;
		
    }

}

    cout<<amnt<<endl;
    comRate=commissionRate*amnt;
    cout<<comRate<<endl;

    cout<<endl<<accountTotal<<endl;
    comRate=commissionRate*accountTotal;
    cout<<comRate;
}

So the last thing I need to do is display monthly totals for each month.

Here's my output:

Enter your filename to save9
File created!
1277 October 3550

1277 November 4855

1277 December 1890
10295
2676.7
2844 November 2900

2844 December 1540
4440
1154.4

14735
3831.1

How should I do it? It needs to be above the total and commission. It needs to display the 3 months for the quarter and their amount for both accounts.
Last edited on
Please read and reformat your post for readability.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Better?
Well, it's still not indented correctly. You shouldn't just indent the code properly for us, you should do it for yourself.
The inconsistent indentation can make what would otherwise be easily read into a mess.
For example,
1
2
3
4
5
6
7
8
9
10
11
12
while (inputFile>> iD>>monthI>>amountofSale)
{

accountTotal+=amountofSale;
commission=commissionRate*amnt;
if(firstiD)	
{
	targetId=iD;
	firstiD = false;	
}
	if (targetId==iD)
	{

is quite hard to read.

So your file contains something like:
1234 11 22.34
3334 4 8.30
etc.


If you need to display the month totals, then you would have some sort of array to keep track of that for each month.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    // Note: replace this istringstream with your ifstream
    istringstream inputFile(
        string("1233 12 8.24\n") +
        string("3001  4 31.25\n") + 
        string("2000  3 7.26\n") +
        string("3001  4 11.11\n") 
        // ...
    );
    
    int id;
    int month;
    double amount;
    
    double total_amounts[12] = {}; // init'd to 0
    
    while (inputFile >> id >> month >> amount)
    {
        // 12 == December, but will be index 11, so subtract 1
        total_amounts[month - 1] += amount;
    }
    
    cout << "Totals:\n";
    for (int i = 0; i < 12; i++)
    {
        cout << "Month " << (i+1) << ": " << total_amounts[i] << '\n';
    }
}

Totals:
Month 1: 0
Month 2: 0
Month 3: 7.26
Month 4: 42.36
Month 5: 0
Month 6: 0
Month 7: 0
Month 8: 0
Month 9: 0
Month 10: 0
Month 11: 0
Month 12: 8.24

(You can replace the number with the actual name of the month using your exists months array)
Last edited on
I need the program to read the file, let's assume they can only interact with the data by using this program. Hence it should be able to take the data and alter it. istringstream alters my program too much for it to work. I thought of using seekg to read the file a second time, is that possible?
Did you even read my comment? I said "replace this istringstream with your ifstream". I simply used a stringstream so that I could make an example without having to tediously write a file to my local disk.

I don't see any reason to read the file two times. Just parse it once and store the necessary information into your arrays. But if you think it will be easier/cleaner to read the file twice, then just open it twice (no need to use seekg, but that would be possible).
Topic archived. No new replies allowed.