problem pulling .txt data and combining multi functions

So I'm tasked with importing a file containing an invoice for given dates with random parts and totals. My program needs to run like a reciept for the given dates;
so:

invoice date: xxx
part#, part name, how many of given part, price, total for that given part
repeat for each part under given date
total........xxxx

I can't seem to get the different lines to print and I am kinda lost at the moment. The project requires me to have all the separate functions and to call them properly. I'm not sure if my functions or the way I am calling them are the problem. Thank you for any help//assistance.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
ofstream fout;
void printReportHeader();
void printInvoiceHeader(string date);
void printLine();
double calcCost();
double accTotal();
void printTotal();

int main()

{
	ifstream inputFile;
	int transactions;
	string date;
	int items, unitCost;
	
	// opening input.txt with data
	inputFile.open("input.txt");
	cout << fixed << setprecision(2);

printReportHeader();		//printing the header function
while (inputFile >> items >> date)
{
	double totalCost = 0;
	printInvoiceHeader(date);	// printing the invoice date

	for (int i = 0; i < 28; i++)
	{
		printLine();				// calls the reference number, name, quantity, price, and total				
		calcCost();					// calls the calculation price*unit
		accTotal();					// calls the acount total for the given invoice
	}
}
	printTotal();				// prints total
	
	return 0;
}

// calculates the quantity * unit cost to get TOTAL COST
double calcCost()
{
	string transactions, date;
	ifstream inputFile;
	int name, invNum;
	
	double totalCost=0;
	double unitCost;
	int quantity;

	inputFile.open("input.txt");
	inputFile >> transactions >> date >>
	invNum >> name >> quantity >> unitCost;

	// takes a the quantity and mulitplies the unit cost 
	// to get the total cost per unit given the quantity
	totalCost = quantity*unitCost; 
	

	return totalCost;
}

// adds up lines to give total cost of invoice
double accTotal()
{
	string transactions, date;
	ifstream inputFile;
	int name, invNum;

	double totalCost = 0;
	double unitCost;
	int quantity;

	inputFile.open("input.txt");
	inputFile >> transactions >> date >>
		invNum >> name >> quantity >> unitCost;

	double newTotalCost;
	newTotalCost = quantity++*unitCost++; // adds up each lines totals and creatates an new end total

	return newTotalCost;
}

//void function that writes the invoice line
void printLine()
{
	int transactions;
	string date, name;
	ifstream inputFile;
	double unitCost, total;
	int quantity;
	int invNum;

	inputFile.open("input.txt");
	inputFile >> transactions >> date >>
		invNum >> name >> quantity >> unitCost;
	
	// prints the line for invoice #, name of par, quantity, and the cost per unit
	cout << invNum << "  " << name << "  " << quantity << "  " << unitCost << endl;			
		
}

//void function to print "Total.. with account total
void printTotal()
{
	
	cout << "Total ................................" << accTotal();	
	cout << endl;

}

//void function for invoice Header with DATE
void printInvoiceHeader(string date)
{
	int transactions;
	
	ifstream inputFile;
	inputFile.open("input.txt");
	inputFile >> transactions >> date;
	cout << "Invoice date: " << date << endl << endl;
}

//Void function for report Heade with Company name
void printReportHeader()
{
	cout << "  Erie Industrial Supply Corporation \n";
}


data pulled from .txt file:
3 2/12/2017
134276 Framis-R 8 7.35
125790 Framis-L 12 15.76
100086 Aglet 395 0.11
2 1/23/2017
135876 Wrench 12 22.50
543287 Henway 4 19.25
3 1/15/2017
161432 Widget 5 6.50
543289 Wodget 10 2.25
876234 Gadget 2 10.75
9 2/16/2017
198765 Corkle 2 12.18
107654 Swale 3 8.75
987987 Sinter 5 6.43
654821 Zangle 6 18.45
432165 Lunule 7 9.60
333221 Fangle 10 58.49
654098 Fwetstock 3 12.65
543210 Furnal 3 9.98
543287 Bobble 12 19.25
6 2/26/2017
654821 Muntin 2 18.45
766443 Finwiddie 9 2.22
120980 Ferkle 100 0.12
123222 Sneedooper 700 1.19
876543 Ferrule 2 345.77
877721 Uberfrock 14 88.93
Can you post the content of your text file?

Last edited on
The problem is that you open the input file in many places and it will always be at the beginning again.
Better is to open it only in main and pass it by reference to the functions.
Topic archived. No new replies allowed.