Printing, opening/closing files, and arrays

Hello. I am having trouble with this code. Would not give me an output and I don't understand why I have some errors. Here is the scenario:

FIRST: Declare all 4 arrays and any other variables that you will need.
Use a "for" loop ( or multiple "for" loops ) to initialize all 4 arrays.

SECOND:
Populate the first 3 arrays of numbers exactly as you did in the first 3 separate programs in Lab#9.
{ Do NOT include any printing from Lab#9, yet. }
I want to see the code to populate all 3 arrays FIRST - before any calculations or printing is done.

THIRD:
Call a function named "populateISBN" that will populate the fourth array and will return
the number of elements that were filled.
"main" only has the call that passes the array, named "ISBN" as an argument.
After the function has executed, the fourth array, "ISBN", will be populated.

FOURTH:

Print column headers in this order:
ISBN Number Wholesale Cost Markup Amount Sales Tax Amount Retail cost

FIFTH:
Use ONE "for" loop to access the array elements ( I.e. using "Parallel Arrays" ), to do the
calculation(s) and to print the values of these per book:
ISBN Number Wholesale Cost Markup Amount Sales Tax Amount Retail cost

Please print one line for each book.

SIXTH:
Print 2 blank lines followed by your name and "Lab#10".


Write what is needed for the function named "populateISBN":
It will need a prototype and a definition. It returns an integer and takes an array of "string" as a parameter.
The body of the function will be the same code for populating the array as you had in Lab#9 Part D.
You are returning the value of the "counter" ( or "counter" minus one. )
Here is my code:

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 // Rebecca Carolina Katz
//Filename: lab10
//Lab 10
// In this program, i am combining all of the code from lab 9 in the function main.

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{	
	// Declarations of all 4 arrays
	double wholesale_cost[25];
	int index=0;
	double MarkUp_Amt[25];
	double Tax_Amt[25];
	int counter;

    // Declarations and opening 3 input files
	ifstream infile_1;
	ifstream infile_2;
	ifstream infile_3;
	
    // opening file for wholesale cost
    infile_1.open("A_Cost.txt");
	// Verify that the file opened correctly: 
	if ( !infile_1 )
	{
		cout << "NO such file as A_Cost.txt" << endl;
		return -1;
	}
	
	
	//initialized the array
	for (index=0;index<25;++index)
	{
		wholesale_cost[index]=0;
	}
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "wholesale_cost" and printing it
	while (!infile_1.eof())
	{
		++index;

	} 
	
	// Close the file 
	infile_1.close();
	
	cout<< "Number of values read: "  << counter  << endl;
	
	for( int counter=0; counter < ++index; ++counter)
	{
		cout << wholesale_cost[counter] << endl; 
	}
	
    // opening the file for MarkUP_Amt
	
	infile_2.open("B_MarkUp.txt");

	// Verify that the file opened correctly: 
	if ( !infile_2 )
	{
		cout << "NO such file as B_MarkUp.txt" << endl;
		return -1;
	}
	
	
	//initialized the array
	for (index=0;index<25;++index)
	{
		MarkUp_Amt[index]=0;
	}
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "B_MarkUp" and printing it
	while (!infile_2.eof())
	{
		infile_2>>MarkUp_Amt[index];
		++index;

	} 
	
	// Close the file 
	infile_2.close();
	
	cout<< "Number of values read: "  << index  << endl;
	
	for( int counter=0; counter < index; ++counter)
	{
		cout << MarkUp_Amt[counter] << endl;
	}
	
	
	// opening the file for Tax_Amt
	
	infile_3.open("C_Tax.txt");

	// Verify that the file opened correctly: 
	if ( !infile_3 )
	{
		cout << "NO such file as C_Tax.txt" << endl;
		return -1;
	}
	
	
	//initialized the array
	for (index=0;index<25;++index)
	{
		Tax_Amt[index]=0;
	}
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "Tax_Amt" and printing it
	while (!infile_3.eof())
	{
		infile_3>>Tax_Amt[25];
		++index;

	} 
	
	// Close the file 
	infile_3.close();
	
	cout<< "Number of values read: "  << index  << endl;
	
	for( int counter=0; counter < index; ++counter)
	{
		cout << Tax_Amt[counter] << endl; 
	}
	
	// Declaration for ISBN
	string ISBN[25];
	int l=0;
	string inputvariable;

	ifstream infile_4;
	
	infile_4.open("D_ISBN.txt");

	// Verify that the file opened correctly: 
	if ( !infile_4 )
	{
		cout << "NO such file as D_ISBN.txt" << endl;
		return -1;
	}
	
	
	//initialized the array
	for (index=0;index<25;++index)
	{
		ISBN[index]="";
	}
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "ISBN" and printing it
	while (!infile_4.eof())
	{
	
		infile_4 >> ISBN[index];
		++index;

	} 
	
	// Close the file 
	infile_4.close();
	
	cout<< "Number of values read: "  << index  << endl;
	
	for( int counter=0; counter < index; ++counter)
	{
		cout << ISBN[counter] <<endl;;
	}
	
	//printing of all 4 arrays
	for (int index=0,index < counter,++index)
	{
		cout<< wholesale_cost[index]<<"i";
		cout<< MarkUp_Amt[index]<<"j";
		cout<< Tax_Amt[index]<<"k"<<endl;
		
    }
	
	cout<<endl;
	cout << "Rebecca Carolina Katz ";
	cout << "Lab 10" << endl;
	
}
1
2
3
4
5
	while (!infile_1.eof())
	{
		++index;

	} 

You did not read anything from the file.

Try like you did for the other files:

1
2
3
4
5
6
	while (!infile_1.eof())
	{
		infile_1>>wholesale_cost[index];
		++index;

	} 


Though I recommend not looping over eof, this seems to be what most teacher teach at the beginning.
Last edited on
1
2
		infile_3>>Tax_Amt[25];
		++index;

This will crash your program. It is going out of bounds.

infile_3>>Tax_Amt[25];
++index did not crash my program...it ran. But this is my new code. Is it okay?

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Rebecca Carolina Katz
// Filename: lab10
// Lab 10
// In this program, i am combining all of the code from lab 9 in the function main.

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;
int populateISBN(string ISBN[]);

int main()
{	
	// Declarations of all 4 arrays
	double wholesale_cost[25];
	double MarkUp_Amt[25];
	double Tax_Amt[25];
	string ISBN[25];
	int index=0;
	int counter;

    // Declarations and opening 3 input files
	ifstream infile_1;
	ifstream infile_2;
	ifstream infile_3;
	
	//Populate the fist 3 arrays
	index=0;
	
    // opening file for wholesale cost
    infile_1.open("A_Cost.txt");
    
	// Verify that the file opened correctly: 
	if ( !infile_1 )
	{
		cout << "NO such file as A_Cost.txt" << endl;
		return -1;
	}
	
	// opening the file for MarkUP_Amt	
	infile_2.open("B_MarkUp.txt");

	// Verify that the file opened correctly: 
	if ( !infile_2 )
	{
		cout << "NO such file as B_MarkUp.txt" << endl;
		return -1;
	}
	
	// opening the file for Tax_Amt
	infile_3.open("C_Tax.txt");

	// Verify that the file opened correctly: 
	if ( !infile_3 )
	{
		cout << "NO such file as C_Tax.txt" << endl;
		return -1;
	}
		
	//Initializing  the  arrays
	for (index=0;index<25;++index)
	{
		wholesale_cost[index]=0;
		MarkUp_Amt[index]=0;
		Tax_Amt[index]=0;
	}		
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "wholesale_cost" and printing it
	while (!infile_1.eof())
	{
		infile_1 >> wholesale_cost[index];
		++index;
	} 
	
	// Close the file 
	infile_1.close();	
	
	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "B_MarkUp" and printing it
	while (!infile_2.eof())
	{
		infile_2 >> MarkUp_Amt[index];
		++index;
	} 
	
	// Close the file 
	infile_2.close();

	index=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "Tax_Amt" and printing it
	while (!infile_3.eof())
	{
		infile_3 >> Tax_Amt[index];
		++index;
	} 
	
	// Close the file 
	infile_3.close();

	index = populateISBN(ISBN);
	
	// Print column headers
	cout << "ISBN Number		Wholesale Cost	Markup Amount	Sales Tax Amount	Retail cost" << endl;
	
	// Print all the array elements
	for( int counter=0; counter < index; ++counter)
	{
		
		cout << ISBN[counter] << "\t" << wholesale_cost[counter]
		<< "\t\t" << MarkUp_Amt[counter]<< "\t\t" <<Tax_Amt[counter] 
		<< "\t\t\t" << (wholesale_cost[counter] + MarkUp_Amt[counter] + Tax_Amt[counter]) <<endl; 
		
	}
	
	
	cout << endl;
	cout << endl;
	cout << "Rebecca Carolina Katz ";
	cout << "Lab 10" << endl;
	
}

// Function populateISBN populates the fourth array
// returns the numbers of elements that were filles

int populateISBN(string ISBN[])
{
	// Declaration variables
	//string ISBN[25];
	int l=0;

	ifstream infile_4;
	
	infile_4.open("D_ISBN.txt");

	// Verify that the file opened correctly: 
	if ( !infile_4 )
	{
		cout << "NO such file as D_ISBN.txt" << endl;
		return -1;
	}
	
		//Initializing  the four array
	for (l=0;l<25;++l)
	{
		ISBN[l]="X";
	}	
	
	l=0;
	// reading in each value from the file until END-OF-FILE
	// putting the value in a array named "ISBN" and printing it
	while (!infile_4.eof())
	{
		infile_4 >> ISBN[l];
		++l;
	} 
	
	// Close the file 
	infile_4.close();
	
	return l;
}
At a quick glance, I do not notice any glaring issues.
Try running it and see if you get the correct output.

I only said that code wouldn't work because it seemed to go out of bounds of your array. Avoid going out of bounds at all costs.

I still don't recommend looping over 'eof' or 'fail'. 'eof' doesn't get set until after you try to read beyond the file, and 'fail' can be set for multiple reasons.
You could try:
1
2
3
4
while (infile >> myarray[index])
{
    ++index;
}


To be safer, try string streams: http://www.cplusplus.com/reference/sstream/stringstream/
How is the format of the files?
Topic archived. No new replies allowed.