Help with infile

Having a hard time getting my project to read from my txt file. Cannot seem to find the issue maybe another set of eyes could help. Thanks in advance. Keep getting my own cannot read message

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
232
233
234
235
236
237
238
  #include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>
using namespace std;

template <class elemType>
class listType
{
public:
bool isEmpty() const;
bool isFull() const;
int getLength() const;
int getMaxSize() const;
void sort();
void print() const;
void insertAt(const elemType& item, int position);
listType(int listSize = 50);
~listType();

protected:
int maxSize;
int length;
elemType *list;
};

template <class elemType>
bool listType<elemType>::isEmpty() const
{
return (length == 0);
}

template <class elemType>
bool listType<elemType>::isFull() const
{
return (length == maxSize);
}

template <class elemType>
int listType<elemType>::getLength() const
{
return length;
}

template <class elemType>
int listType<elemType>::getMaxSize() const
{
return maxSize;
}

template <class elemType>
listType<elemType>::listType(int listSize)
{
maxSize = listSize;
length = 0;
list = new elemType[maxSize];
}

template <class elemType>
listType<elemType>::~listType()
{
delete [] list;
}

template <class elemType>
void listType<elemType>::sort()
{
int i, j;
int min;
elemType temp;

for(i = 0; i < length; i++)
{
min = i;
for(j = i; j < length; ++j)
if (list[j] < list[min])
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
}

template <class elemType>
void listType<elemType>::print() const
{ //print function
int i;
for(i = 0; i < length; ++i)
cout << list[i];
cout << endl;
}

template <class elemType>
void listType<elemType>::insertAt(const elemType& item, int position)
{
assert(position >= 0 && position < maxSize);
list[position] = item;
length++;
}


class Stock
{

private:
string stock_symbol;
double opening_price;
double closing_price;
double today_high;
double today_low;
int number_of_shares;
double previous_close_price;
static double total_assets;
double percent_gain_loss;
public:

void setStock(string stock_symbol,double opening_price,double closing_price,
double today_high, double today_low, int number_of_shares, double previous_close_price)
{
this->stock_symbol = stock_symbol;
this->opening_price = opening_price;
this->closing_price = closing_price;
this->today_high = today_high;
this->today_low = today_low;
this->number_of_shares = number_of_shares;
this->previous_close_price = previous_close_price;
}

void Print()
{
cout <<"Stock name is " << stock_symbol << endl;
}

void show_different_prices()
{
cout <<"today's opening price was "<<opening_price <<", the closing was "<<closing_price <<", today's high was " <<
today_high << ", today's low was " <<today_low << " Previous closing price is " << previous_close_price <<endl;
}

void calculate_gain_loss()
{
percent_gain_loss = (closing_price-previous_close_price)/previous_close_price*100;
}

void show_number_of_shares()
{
cout <<"Number of share is " << number_of_shares << endl;
}

bool operator> (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)>0);
}
bool operator>= (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)>=0);
}
bool operator< (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)<0);
}
bool operator<= (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)<=0);
}
bool operator==(Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)==0);
}

friend ostream& operator<< (ostream &out, Stock &cStock);

friend istream& operator>> (istream &in, Stock &cStock);
static double getTotalAssets()
{
return total_assets;
}
};

istream& operator>>(istream &in, Stock &cStock)
{

in >> cStock.stock_symbol;
in >> cStock.opening_price;
in >> cStock.closing_price;
in >> cStock.today_high;
in >> cStock.today_low;
in >> cStock.previous_close_price;
in >> cStock.number_of_shares;
cout << cStock.total_assets << endl;
cStock.total_assets+=cStock.number_of_shares*cStock.closing_price;
return in;
}
ostream& operator<< (ostream &out, Stock &cStock)
{
cStock.calculate_gain_loss();

out << setw(8) << left << cStock.stock_symbol <<right << fixed << setprecision(2) << setw(8) <<
cStock.opening_price<< fixed << setprecision(2) << setw(8) <<
cStock.closing_price<< fixed <<setprecision(2) << setw(8) <<
cStock.today_high<< fixed << setprecision(2) << setw(8) <<
cStock.today_low<< fixed <<setprecision(2) << setw(8) <<
cStock.previous_close_price<< fixed << setprecision(2) << setw(8) <<
cStock.percent_gain_loss<< fixed << setprecision(2) << setw(12) <<
cStock.number_of_shares<< endl;
return out;
}
double Stock::total_assets = 0.0;
int main()
{
ifstream infile("D:\\Stockdata.txt");
listType<Stock> stock_array(50);
Stock local_stock;
int count = 0;
if(!infile)
{
cout <<"Cannot open file StockData, now exiting from program ";
return 0;
}
while(!infile.eof())
{
infile >> local_stock;
stock_array.insertAt(local_stock,count++);
}
stock_array.sort();
cout <<"*************First Investors Heaven*****************"<<endl;
cout <<"*************Financial Report*****************"<<endl;
cout <<"Stock Today Previous Percent " << endl;
cout <<"Symbol Open Close High Low Close Gain Volume" << endl;
cout <<"------ ----- ----- ----- ----- -------- ------- ------"<< endl;
stock_array.print();
cout <<"Closing Assets: $"<< fixed << setprecision(2) << Stock::getTotalAssets()<< endl;
cout <<"_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"<<endl;
system("pause");
return 0;
}
.
Last edited on
After line 223, output local_stock. Is it what you expect?
still can not get it to read my text file. have tried he full file path, putting the file into the eclipse project folder
How do you know it's not reading the text file? We're not psychic. We can't see your screen, we can't see your error messages, all we have is what you tell us; so tell us how you know it's not reading the text file.
Last edited on
Cannot open file StockData, now exiting from program

This is the message I get when I run my program. Which is what is supposed to do if the wrong file or no file is read.
Doesn't seem to be the code, check the file and spelling, including case, spaces and file extension.
And read permissions.
Topic archived. No new replies allowed.