EOF problem

Yes I've been working on this project for class and I've been having a bit of trouble.

The eof is working fine and it is outputting the last file in the window but it isn't adding the last number in the file. Bah i suck at describing this. 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;

//Declaring Methods
void Heading();
void printCityTotal(string,int&, int&, int);
void printCountyTotal (string,int&, int &, int);
void printDistrictTotal (string,int&, int &, int);;




int main()
{
//Initializing all data types

string strFill1;  
string strDistrictNumber;
string strCountyNumber;
string strCityNumber;
string strAccountNumber;
string strAccountName;
int BondAmount=0;
char ch;
int s;
int cityTotal=0;
int countyTotal=0;
int districtTotal=0;
int finalTotal=0;
string strDistrictNumberOld;
string strCityNumberOld;
string strCountyNumberOld;




//Opens the file
ifstream inFile;
inFile.open("FBRKUNSORT2.TXT", ios::in);
Heading();
//Begin the loop
if (!inFile.eof())
{


//getting the first line
getline(inFile, strDistrictNumber, '#');
getline(inFile, strCountyNumber, '#');
getline(inFile, strCityNumber, '#');
getline(inFile, strFill1, '#');
getline(inFile, strFill1, '#');
getline(inFile, strFill1, '#');
getline(inFile, strFill1, '#');
getline(inFile, strAccountNumber, '#');
getline(inFile, strFill1, '\n');
//setting everything to old
strDistrictNumberOld=strDistrictNumber;
strCountyNumberOld=strCountyNumber;
strCityNumberOld=strCityNumber;

  while (!inFile.eof())
  {
	  //Adding the BondAmount to city total
	  cityTotal+=BondAmount;

//Made a new variable called s(For Switch) to take in AccountNumber so there is a variable getting passed into the switch
s=(atoi(strAccountNumber.c_str()));

//begin the switch statement
switch(s)
{
	case 1: {
		strAccountName = "HIGHWAY CONTRSTUCTION";
		BondAmount = 300000;
		break;}
	case 2:{
		strAccountName = "PARKS & RECREATION";
		BondAmount = 270000;
		break;}
	case 3:{
		strAccountName = "MENTAL HEALTH";
		BondAmount = 150000;
		break;}
	case 4:{
		strAccountName = "SECONDARY SCHOOLS";
		BondAmount = 450000;
		break;}
	case 5:{
		strAccountName = "COLLEGE & UNIVERSITY";
		BondAmount = 325000;
		break;}
	case 6:{
		strAccountName = "POLICE-FIRE";
		BondAmount = 450000;
		break;}
}
	//Begin the Control Breaks
	  if (strDistrictNumberOld != strDistrictNumber)
	  {	
	     //Calling the Print Functions
		 printCityTotal(strCityNumberOld,countyTotal, cityTotal, BondAmount);
		 printCountyTotal (strCountyNumberOld, districtTotal,countyTotal,BondAmount);
		 printDistrictTotal (strDistrictNumberOld,finalTotal,districtTotal,BondAmount);

		 //resetting old to new
		 strDistrictNumberOld=strDistrictNumber;
		 strCountyNumberOld=strCountyNumber;
		 strCityNumberOld=strCityNumber;


	  }
	  else if (strCountyNumberOld != strCountyNumber)
	  {		 // "
		 printCityTotal(strCityNumberOld,countyTotal, cityTotal, BondAmount);
		 printCountyTotal (strCountyNumberOld, districtTotal,countyTotal,BondAmount);

		 //"
		 strCountyNumberOld=strCountyNumber;
		 strCityNumberOld=strCityNumber;
	  }
	  else if (strCityNumberOld != strCityNumber)
	  {	  //"	
		  printCityTotal(strCityNumberOld,countyTotal, cityTotal, BondAmount);

		  //"
		  strCityNumberOld=strCityNumber;
		   
		  
	  };
	//prints
cout.imbue(locale( ""));	
cout <<left
	<< setw(10) << strDistrictNumber
	<< setw(10) << strCountyNumber
	<< setw(10) << strCityNumber
	<< setw(10) << strAccountNumber
	<< setw(25) << strAccountName << "$"
	<< setw(13) << BondAmount
	<< endl;
	//Gets Line
	getline(inFile, strDistrictNumber, '#');
	getline(inFile, strCountyNumber, '#');
	getline(inFile, strCityNumber, '#');
	getline(inFile, strFill1, '#');
	getline(inFile, strFill1, '#');
	getline(inFile, strFill1, '#');
	getline(inFile, strFill1, '#');
	getline(inFile, strAccountNumber, '#');
	getline(inFile, strFill1, '\n');
	
  }


  		  cout <<right << "\n"<< setw(25) << "CITY" << setw(10) <<"#"<< strCityNumberOld << setw(9) << "TOTAL"<< setw(15) <<"$"<< cityTotal <<"*"<<"\n\n";
		  
		  cout <<right << "\n"<< setw(25) << "COUNTY" << setw(10) <<"#"<< strCountyNumberOld << setw(10) << "TOTAL"<< setw(15) <<"$"<<countyTotal <<"**"<<"\n\n";
		  
		  cout <<right << "\n"<< setw(25) << "DISTRICT" << setw(10) <<"#"<< strDistrictNumberOld << setw(10) << "TOTAL"<< setw(15) <<"$"<< districtTotal<<"***" <<"\n\n";

		  cout <<right<< "\n"<< setw(25) << "FINAL" << setw(22) << "TOTAL"<< setw(15) <<"$"<< finalTotal <<"\n\n";
//End of while loop
//Closes open file

		 
inFile.close();
}
else
  cout << "Could not read the file";



cout << endl;
cout << "                              END OF REPORT" << endl;

system("PAUSE");

ch = _getch();
return 0;
}
//Print Functions
void printCityTotal (string strCityNumberOld,int &countyTotal, int &cityTotal, int BondAmount)
{
  	cout <<right << "\n"<< setw(25) << "CITY" << setw(10) <<"#"<< strCityNumberOld << setw(9) << "TOTAL"<< setw(15) <<"$"<< cityTotal<<"*"<<"\n\n";
		  //strCityNumberOld=strCityNumber;
		countyTotal+=cityTotal;	
		cityTotal=0;
};
void printCountyTotal (string strCountyNumberOld, int &districtTotal, int &countyTotal, int BondAmount)
{
	//printCityTotal (string strCityNumberOld, int &cityTotal, int BondAmount);
  	cout <<right << "\n"<< setw(25) << "COUNTY" << setw(10) <<"#"<< strCountyNumberOld << setw(10) << "TOTAL"<< setw(15) <<"$"<<countyTotal<<"**" <<"\n\n";
		 //strCountyNumberOld=strCountyNumber;
		 districtTotal+=countyTotal;
		countyTotal=0;
};
void printDistrictTotal (string strDistrictNumberOld, int &finalTotal, int &districtTotal, int BondAmount)
{
		  cout <<right << "\n"<< setw(25) << "DISTRICT" << setw(10) <<"#"<< strDistrictNumberOld << setw(10) << "TOTAL"<< setw(15) <<"$"<< districtTotal<<"***" <<"\n\n";
		  //strDistrictNumberOld=strDistrictNumber;
		  finalTotal+=districtTotal;
		 districtTotal=0;
};


//Header
void Heading()
{
	cout << setw(33) << "MICHAEL MELENDEZ CIS PROJECT 1"<<"\n\n";
	cout << setw(49) << "PROPOSED BOND ISSUES" << endl;
	cout << "  " << endl;
	cout <<left
	<<setw(10) << "DISTRICT"
	<< setw(10)<<"COUNTY"
	<< setw(10)<<"CITY"
	<< setw(10)<<"ACCOUNT"
	<< setw(25)<<"ACCOUNT"
	<< setw(13)<<"BOND"<<endl;
	cout <<left
	<<setw(10) << "NUMBER"
	<< setw(10)<<"NUMBER"
	<< setw(10)<<"NUMBER"
	<< setw(10)<<"NUMBER"
	<< setw(25)<<"NAME"
	<< setw(13)<<"AMOUNT";
	cout << "                                                                                " << endl;
}




Last edited on
This is the stuff it's reading


10#01#01 #410#ALBERS STEVEN      #FRONT DISK BRAKES   #4#2#2034031045
10#01#01 #561#CANATERRA CANDY    #HEAD LIGHTS         #1#6#3045075105
10#01#01 #765#SCHENEY MAXINE     #NYLON GAS PEDALS    #1#1#4034007016
10#01#01 #813#CRAIG MARY LOU     #BUCKET SEAT         #2#4#3457010005
10#03#01 #887#DUFFY MARCUS       #1" TRAILER HITCH    #1#2#2356002010
10#03#01 #914#EPPLY DERVIS       #TORQUE WRENCHES     #4#1#3056110213
10#03#01 #957#FIEDLER FERN       #4 PLY RADIAL TIRES  #1#5#0345056075
10#05#02 #045#GARCIA FRANCIS     #6' JUMPER CABLES    #2#2#1233004011
10#05#02 #076#KOON BARNEY L.     #SETS OF 4 HEX NUTS  #3#1#0340005017
10#05#02 #189#KOWALSKI PATRICK   #5" WINDSHEILD BLADES#3#3#0567023020
10#05#02 #201#LICKFIELD SANDRA   #PLASTIC FLOOR MATS  #6#6#0450047192
10#05#02 #250#LUBNER SALLY R.    #WOOD STEERING WHEELS#5#4#5459102365
10#06#02 #275#MARKQUARDT MICHAEL #POINTS AND PLUGS    #6#1#3455002078
10#06#02 #323#MARTIN JUSTIN B.   #LOCK DE-ICER        #2#3#0983010045
10#06#02 #357#MCGILL MARTIN J.   #MOTORALL 10W30 OIL  #3#4#5676023056
10#06#02 #789#MEEGAN JULIA R.    #GENERATORS          #3#6#5678090900
10#06#03 #578#MUFFINS ROBERT L.  #1/2" VACUMM LINES   #1#1#6787004098
10#06#03 #971#NAPOLITAN SHEILA R.#METRIC SOCKET SETS  #4#5#0123103134
10#07#03 #989#NOWICKI OZZIE L.   #FLOOR CLEANER       #6#2#3456004021
10#07#04 #456#PEACOCK GERALD M.  #2 TON CAR RAMPS     #6#1#0045010024
10#07#04 #692#PEREZ MARTINIQUE A.#TRANSMISSION KITS   #2#5#0173080076
10#07#04 #789#POSTEN THOMAS E.   #OIL PANS            #5#4#1234011078
10#07#04 #987#REESES LAURA W.    #OPERATORS MANUALS   #5#2#2894001099
10#07#05 #139#RENO STEVEN B.     #HI/LO DIMMER SWITCH #6#1#0639010020
10#07#05 #983#ROTHCHILD RUTH C.  #RACING STRIPE KITS  #1#3#3404045089
10#08#05 #999#SAMMS CARLA P.     #OIL CHANGE KIT      #6#6#9234090100
10#08#06 #134#SCHMIDT CHUCK H.   #INTERIOR LIGHT BULBS#3#1#2302004050
10#08#06 #297#SHARP CRAIG I.     #SET OF 2 FOG LIGHTS #2#3#7809001008
10#08#06 #356#SHORT BRENDA B.    #DISTRIBUTOR CAPS    #4#2#9834010023
10#08#06 #489#SKINNER CARL S.    #TIMING GEARS        #2#5#0789110023
10#08#06 #598#SPRINGSTEIN ALBERT #SECURITY SYSTEM     #4#6#0929010067
10#08#06 #701#STROH ALEXIS M.    #CRUISE CONTROLS     #3#4#2856055067
20#01#07 #980#SWEENEY ROGER W.   #SET OF 4 HUB CAPS   #4#2#9834010023
20#01#08 #379#TEEPLE THOMAS T.   #AXLE GREASE (GALLON)#5#1#2347001023
20#01#08 #567#TOMCHUCK FREDERICK #NO-WIPE CAR POLISH  #1#3#0375004056
20#01#08 #673#TOMLIN PETER S.    #CAR WAXERS          #2#6#9845090020
20#01#08 #674#TOWNLEY ASTIN R.   #GREASE-B-GONE WASHER#4#5#5628034056
20#01#08 #732#TOWNS BEVERELY     #SUNNY SUNROOF KIT   #4#2#2345011045
20#03#08 #845#TRAKSO ALLEN R.    #ROAD MAPS           #6#1#0304001005
20#03#08 #913#TRUSKOWSKI MIKE    #2-WAY OIL FILTER    #1#2#3040010052
20#03#08 #957#TURNER CHARLES A.  #ENGINE BLOCKS       #5#4#0378402098
20#03#08 #975#TYLER MARY M.      #TRANSMISSION FLUID  #5#3#0923045045
20#05#09 #003#ULMER FRED F.      #NO-SLIP BRAKE FLUID #2#4#0450050067
20#05#09 #156#URMY AMY A.        #NO-LEAK RADIATORS   #4#1#9340120344
20#05#09 #245#VARTY VERNON S.    #SPARKLES PAINT      #6#2#0098009009
20#05#09 #347#VINCENT BOBBY L.   #CHROME POLISH       #3#6#2984019090
20#07#09 #467#WADHAM LARRY L.    #VINYL TO KITS       #1#4#0086087078
20#07#09 #610#WALKER JERRY F.    #6" ROUND HEADLIGHTS #6#5#5870087090
20#07#09 #763#WENDT WENDY Y.     #NO-FUZZ FUZZ BUSTER #4#2#4598030030
20#07#09 #908#ZELLER ARTHUR      #GLASS WINDOWS       #1#1#8890200230


Another problem I'm having is the last control break the District total is 0 while the rest of the file is fine. Why is that?
Last edited on
I haven't analysed the code thoroughly but if getline(inFile, strFill1, '\n'); is the last line, and input, in the while loop then the while loop is terminating immediately after the last line from the file is read.

As all of the processing code is above this line the last read input never gets processed.
I haven't analysed the code thoroughly but if getline(inFile, strFill1, '\n'); is the last line, and input, in the while loop then the while loop is terminating immediately after the last line from the file is read.

As all of the processing code is above this line the last read input never gets processed.


That only fills the numbers at the end to blank, would that mess up the whole while loop because of that? I am trying to figure out what is wrong with this and I just can't seem to figure it out.
You are reading strAccountNumber at line # 153, processing it at line #72.
After the last strAccountNumber is read from file the EOF is reached, the control then goes to line #66, the condition is no longer true so the while loop terminates.

This means the last read of strAccountNumber is wasted.

convert the while loop to a do-while loop and move the getlines (lines 146-154) to start of the loop and remove the lines 52-60
The
1
2
else
  cout << "Could not read the file";


is giving me a error when switching it to a do while.

when I have it just as a while loop and i have 146-156 at the beginning of the loop, my code COMPLETELY messes up.
Error as in compilation error?
Did you accidentally remove the
1
2
if (!inFile.eof())
{
?
the error I get is "Error: expected 'while' "
I get the error on line 172 when I change the while to do while like suggested. I really don't know what to do at this point x.x
replace line 66, in the original code, with "do" and replace the ending "}" of the original while loop with }while (!inFile.eof());

structure of do-while loop is
1
2
3
4
do 
{
    //Something 
}while( condition-is-true );


This will eliminate duplicated getlines in your code and put the input before the processing as you put these lines at start of the loop.

Hope this solves your error and the original problem
Topic archived. No new replies allowed.