OK, I admit I thought I had it!

This thing is killing me. I cannot get the thing to read into the variables for my class. This stinking thing is due by Wednesday and I am very stuck, probably because I am very stupid. This is my input file content:
ABC 123.45 130.95 132.00 125.00 120.50 8.67% 10000
AOLK 80.00 75.00 82.00 74.00 83.00 -9.64% 5000
CSCO 100.00 102.00 105.00 98.00 101.00 0.99% 25000
IBD 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
MSET 120.00 140.00 145.00 140.00 115.00 21.74% 30920

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

class stockType
{
public:
       void setStock (char, double, double, double, double, double, double);
       void getStock (char&, double&, double&, double&, double&, double&, double&)const;
       void printStock() const;//works
       void showPriceDiff();//works
       double calcPercentGain();//works
       double showShares(); //works
       friend istream& operator >> (istream &in, const stockType &cStock);// Dawn Brownell-Adie ****
       friend ostream& operator<< (ostream &out, const stockType &cStock);// Dawn Brownell-Adie ****
       double getTotalAssets();// Dawn Brownell-Adie ****
       stockType(char, double, double, double, double, double, double);
       stockType();//works

        char ss;
        double op, cp, th, tl, pc, sv;
        double total_assets;// Dawn Brownell-Adie ****
};
void stockType::setStock (char symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double volume)
{//*Daniel De Gasperis***************************************************************
                                                                                  //*
      ss = symbol;                                                                //*
                                                                                  //*
      op = openingPrice;                                                          //*
                                                                                  //*
      cp = closingPrice;                                                        //*
                                                                                  //*
      th = todayHigh;                                                             //*
                                                                                  //*
      tl = todayLow;                                                              //*
                                                                                  //*
      pc = prevClose;                                                             //*
                                                                                  //*
      sv = volume;                                                                //*
                                                                                  //*
}//*Daniel De Gasperis***************************************************************

void stockType::getStock(char& symbol, double& openingPrice, double& closingPrice, 
double& todayHigh, double& todayLow, double& prevClose, double& volume) const 
{ 
     symbol = ss; openingPrice = op; closingPrice = cp; todayHigh = th;
     todayLow = tl; prevClose = pc; volume = sv;
}

void stockType::printStock() const 
{ 
       cout << ss; 
       cout << op;
       cout << cp;
       cout << th;
       cout << tl;
       cout << pc;
       cout << sv; 
} 

void stockType::showPriceDiff()// Nichole Knowles*****************************************
{                                                                                      //*
cout << "Today's opening price was " << op << ", and the closing was " << cp << endl;  //*
cout << "Today's high price was " << th << ", and the low price was " << tl << endl;   //*
cout << "While the previous closing price was " << pc << endl;                         //*
}                                                                                      //*
double stockType::calcPercentGain()                                                    //*
{                                                                                      //*
return (cp - pc) / pc * 100;                                                           //*
} // Nichole Knowles**********************************************************************
double stockType::showShares()
{
return sv;     
}     
istream& operator >> (istream& in, stockType& cStock)// Dawn Brownell-Adie ******

{

// Since operator>> is a friend of the stockType class, we can access StockType members directly.

       in >> cStock.ss;

       in >> cStock.op;

       in >> cStock.cp;

       in >> cStock.th;

       in >> cStock.tl;

       in >> cStock.pc;

       in >> cStock.sv;

       // cout << cStock.total_assets << endl;

          cStock.total_assets += cStock.sv*cStock.pc;

       return in;// Dawn Brownell-Adie ******************************************

}
ostream& operator<< (ostream& out, stockType& cStock)// Dawn Brownell-Adie ******

{

       cStock.calcPercentGain();

// operator<< is a friend of the StockType class, we can access StockTypes members directly.

       out << setw(8) << left << cStock.ss << right << fixed << setprecision(2) << setw(8) <<

              cStock.op << fixed << setprecision(2) << setw(8) <<

              cStock.cp << fixed << setprecision(2) << setw(8) <<

              cStock.th << fixed << setprecision(2) << setw(8) <<

              cStock.tl << fixed << setprecision(2) << setw(8) <<

              cStock.pc << fixed << setprecision(2) << setw(12) <<

              cStock.sv << fixed << setprecision(2) <<endl;

       return out;// Dawn Brownell-Adie ****

}
 double stockType::getTotalAssets()// Dawn Brownell-Adie ****

{ 

 return 0.0;// Dawn Brownell-Adie ****

}
stockType::stockType(char symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double volume)
{
      if ('A' <= symbol && symbol < 'z') 
      ss = symbol; else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose & prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (-999999 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;
}
stockType::stockType() 
{ 
    ss = 'A'; 
    op = 0; 
    cp = 0;
    th = 0;
    tl = 0;
    pc = 0;
    sv = 0; 
};


int main()

{
        char ss;
        double op, cp, th, tl, pc, sv;
 stockType stock;    
 cin>>ss>>op>>cp>>th>>tl>>pc>>sv;
 
ifstream in("Stocks.txt");   
stock.printStock();

 system ("pause");
 return 0;    
}    
Look at the order of executable statements in main() ...

- line 179: you declare a stockType object;
- line 180: you read stock details; (from the KEYBOARD, not file)
- line 182: you (belatedly?) open the input file ... but you don't try to read it
- line 183: you try to print out a stockType object (which hasn't been assigned any data)

Try first to write out - IN WORDS, NOT AS C++ STATEMENTS - what you would do in sequential order. Almost every function that you need is already there for you.
There are a number of mismatches. Some within the code where things are not consistent with itself. Another most important one is the mismatch of the file contents with the data fields in the class.
Let's look at the first line of the input file:
ABC 123.45 130.95 132.00 125.00 120.50 8.67% 10000
The layout goes like this:
1
2
3
4
5
6
7
8
9
    string ABC
    double 123.45
    double 130.95
    double 132.00
    double 125.00 
    double 120.50 
    double 8.67
    char % 
    int 10000

and it is being read into the following variables
1
2
3
4
5
6
7
    char ss;
    double op;
    double cp;
    double th;
    double tl;
    double pc;
    double sv;

The first problem is that a three or four letter string cannot be stored inside a single character. You could discard the other characters, but I'd suggest making ss into a type string instead of char.


The second problem is what happens at the end of each line? there is a remaining % symbol and an integer value. You may want to just ignore these - that is code an ignore(1000, '\n') statement to ignore everything up to and including the next newline.

By the way, you should place all of those variables in the private: section of the class declaration, at the moment they are all public.

Next problem.
The two friend functions.
17
18
    friend istream& operator >> (istream &in, const stockType &cStock);
    friend ostream& operator<< (ostream &out, const stockType &cStock);

Now look at the corresponding function definitions:
 
    istream& operator >> (istream& in, stockType& cStock) // line 79 

 
    ostream& operator<< (ostream& out, stockType& cStock) // line 106 


Both the declarations have the keyword const before the stockType, and neither of the actual definitions do.
That's a major problem. The compiler cannot tie them together, they appear completely different. So which is which? Well the input operator >> will need to modify the stockType object, therefore it cannot be constant. Conversely the output operator << should not be allowed to modify the object, therefore it should be const.
The correct declarations are:
1
2
3
4
    friend istream& operator >> (istream &in, stockType &cStock);
    friend ostream& operator << (ostream &out, const stockType &cStock);
istream& operator >> (istream& in, stockType& cStock)
ostream& operator<< (ostream& out, const stockType& cStock)



Now to start with, you might want to simply read in the contents of the file and display it, to check everything is working. main() would look like this:
1
2
3
4
5
6
7
8
9
int main()
{
    stockType stock;    
    
    ifstream in("Stocks.txt");  
 
    while (in >> stock)
        cout << stock << '\n';
}   

Last edited on
O.K. I have made every change that you both have suggested and it compiles but it is not doing cout << stock << "\n"; I know if I can just get this to input correctly the rest should go relatively smooth. Looking forward to the day!
Here is the code as it stands now.

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
// Kenneth Clark

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;



 



class stockType
{
public:
       void setStock (string, double, double, double, double, double, char, int);
       void getStock (string&, double&, double&, double&, double&, double&, char&, int&)const;
       void printStock() const;//works
       void showPriceDiff();//works
       double calcPercentGain();//works
       double showShares(); //works
       friend istream& operator >> (istream &in, stockType &cStock);// Dawn Brownell-Adie ****
       friend ostream& operator << (ostream &out, const stockType &cStock);// Dawn Brownell-Adie ****
       double getTotalAssets();// Dawn Brownell-Adie ****
       stockType(string, double, double, double, double, double, char, int);
       stockType();//works

     string ss;
        double op, cp, th, tl, pc;
        char pp;
        int sv;
    
        double total_assets;// Dawn Brownell-Adie ****
};
void stockType::setStock (string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, char percent, int volume)
{//*Daniel De Gasperis***************************************************************
                                                                                  //*
      ss = symbol;                                                                //*
                                                                                  //*
      op = openingPrice;                                                          //*
                                                                                  //*
      cp = closingPrice;                                                        //*
                                                                                  //*
      th = todayHigh;                                                             //*
                                                                                  //*
      tl = todayLow;                                                              //*
                                                                                  //*
      pc = prevClose; 
      
      pp = percent;                                                            //*
                                                                                  //*
      sv = volume;                                                                //*
                                                                                  //*
}//*Daniel De Gasperis***************************************************************

void stockType::getStock(string& symbol, double& openingPrice, double& closingPrice, 
double& todayHigh, double& todayLow, double& prevClose, char& percent, int& volume) const 
{ 
     symbol = ss; openingPrice = op; closingPrice = cp; todayHigh = th;
     todayLow = tl; prevClose = pc; percent = pp; volume = sv;
}

void stockType::printStock() const 
{ 
       cout << ss; 
       cout << op;
       cout << cp;
       cout << th;
       cout << tl;
       cout << pc;
       cout << pp;
       cout << sv; 
} 

void stockType::showPriceDiff()// Nichole Knowles*****************************************
{                                                                                      //*
cout << "Today's opening price was " << op << ", and the closing was " << cp << endl;  //*
cout << "Today's high price was " << th << ", and the low price was " << tl << endl;   //*
cout << "While the previous closing price was " << pc << endl;                         //*
}                                                                                      //*
double stockType::calcPercentGain()                                                    //*
{                                                                                      //*
return (cp - pc) / pc * 100;                                                           //*
} // Nichole Knowles**********************************************************************
double stockType::showShares()
{
return sv;     
}     
istream& operator >> (istream& in, stockType& cStock)// Dawn Brownell-Adie ******

{

// Since operator>> is a friend of the stockType class, we can access StockType members directly.

       in >> cStock.ss;

       in >> cStock.op;

       in >> cStock.cp;

       in >> cStock.th;

       in >> cStock.tl;

       in >> cStock.pc;
       
       in >> cStock.pp;

       in >> cStock.sv;

       // cout << cStock.total_assets << endl;

          cStock.total_assets += cStock.sv*cStock.pc;

       return in;// Dawn Brownell-Adie ******************************************

}
ostream& operator<< (ostream& out,const  stockType& cStock)// Dawn Brownell-Adie ******

{



// operator<< is a friend of the StockType class, we can access StockTypes members directly.

       out << setw(8) << left << cStock.ss << right << fixed << setprecision(2) << setw(8) <<

              cStock.op << fixed << setprecision(2) << setw(8) <<

              cStock.cp << fixed << setprecision(2) << setw(8) <<

              cStock.th << fixed << setprecision(2) << setw(8) <<

              cStock.tl << fixed << setprecision(2) << setw(8) <<

              cStock.pc << fixed << setprecision(2) << setw(2) << 
              
              cStock.pp << fixed << setprecision (2) << setw(12) <<

              cStock.sv << fixed << setprecision(2) <<endl;

       return out;// Dawn Brownell-Adie ****

}
 double stockType::getTotalAssets()// Dawn Brownell-Adie ****

{ 

 return 0.0;// Dawn Brownell-Adie ****

}
stockType::stockType(string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, char percent, int volume)
{
      if ("A" <= symbol && symbol < "z") 
      ss = symbol; else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose & prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (pp == '%')
      pp = percent;
      else pp = 0;
      if (-999999 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;
}
stockType::stockType() 
{ 
    ss = 'A'; 
    op = 0; 
    cp = 0;
    th = 0;
    tl = 0;
    pc = 0;
    pp = 0;
    sv = 0; 
};

 int main()
{

    
    stockType stock;    
    
    ifstream in("Stocks.txt");  
 
    while (in >> stock)
        cout << stock << "\n";

 system ("pause");
 return 0;    
}    
Assuming the same input file is still being used, then something doesn't work in the operator >> function.

ABC    --> ss  string ok 
123.45 --> op  double ok
130.95 --> cp  double ok
132.00 --> th  double ok
125.00 --> tl  double pk
120.50 --> pc  double ok
8      --> pp  char   ok --- but not as intended
.67%   --> sv  int    fail not an integer 
10000   -             not processed


It seems there is another double value which needs to be read.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       in >> cStock.ss;

       in >> cStock.op;
       
       in >> cStock.cp;
       
       in >> cStock.th;

       in >> cStock.tl;

       in >> cStock.pc;
       
       in >> cStock.dummy;
           
       in >> cStock.pp;

       in >> cStock.sv;

where dummy is another variable of type double that I added just to get it to match the input file.

By the way, there's a small typing error at line 173
 
if (0 <= prevClose & prevClose < 9999)

the & operator should be &&.


My G-D, Chervil you are right there is a variable missing. I'll fix that, I have already fixed the typo. Then back to the main issue I guess. Thanks
This seems to be working. I have but one question now. in the definition for the class StockType setStock is the order correct?

is it : ss = symbol;

or should it be: symbol = ss;

and by the way thank you sooo much!

Ken.
You're welcome.

ss = symbol; appears correct - it is consistent with the rest of the code in that function. Is there a particular reason, such as incorrect results, for the doubt?
No real reason. Not getting errors it is just that it appears in the text both ways and I wasn't sure which was best. Hey I think I got the rest working. Could you take a look, because the functions seem to work but the cout << stock output looks different now. for some reason the pg is displaying twice once before the ss and once in its proper place. It is not effecting the functions it just is a little disturbing to look at. I should mention that I am very greatful for you taking the time to look at this when you don't have to. Here is the updated 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
// Kenneth Clark

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;


class stockType
{
public:
       void setStock (string, double, double, double, double, double, double, char, int);
       void getStock (string&, double&, double&, double&, double&, double&, double&, char&, int&)const;
       void printStock() const;//works
       void showPriceDiff();//works
       void calcPercentGain();//working
       void showShares(); //working
       friend istream& operator >> (istream &in, stockType &cStock);// Dawn Brownell-Adie ****
       friend ostream& operator << (ostream &out,  stockType &cStock);// Dawn Brownell-Adie ****
       void getTotalAssets();// working ****// Dawn Brownell-Adie ****
       stockType(string, double, double, double, double, double, double, char, int);
       stockType();//works

     string ss;
        double op, cp, th, tl, pc, pg;
        char pp;
        int sv;
    
        double total_assets;// Dawn Brownell-Adie ****
};
void stockType::setStock (string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double percentGain, char percent, int volume)
{//*Daniel De Gasperis***************************************************************
                                                                                  //*
      if ("A" <= symbol && symbol < "z") 
      ss = symbol; 
      else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose && prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (-999999 <= percentGain && percentGain < 9999)
      pg = percentGain;
      else pg = 0;
      if (pp == '%')
      pp = percent;
      else pp = 0;
      if (0 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;                                                                //*
                                                                                  //*
}//*Daniel De Gasperis***************************************************************

void stockType::getStock(string& symbol, double& openingPrice, double& closingPrice, 
double& todayHigh, double& todayLow, double& prevClose, double& percentGain, char& percent, int& volume) const 
{ 
     symbol = ss; openingPrice = op; closingPrice = cp; todayHigh = th;
     todayLow = tl; prevClose = pc; percentGain = pg; percent = pp; volume = sv;
}

void stockType::printStock() const 
{ 
       cout << ss << " " << op << " " << cp << " " << th << " " << tl << " " << pc << " " 
            << pg << pp << " " << sv << " "; 
} 

void stockType::showPriceDiff()// Nichole Knowles*****************************************
{                                                                                      //*
cout << "Today's opening price was " << op << ", and the closing was " << cp << endl;  //*
cout << "Today's high price was " << th << ", and the low price was " << tl << endl;   //*
cout << "While the previous closing price was " << pc << endl;                         //*
}                                                                                      //*
void stockType::calcPercentGain()                                                    //*
{                                                                                      //*
cout << (cp-pc)/pc*100;                                                           //*
} // Nichole Knowles**********************************************************************
void stockType::showShares()
{
cout << sv;     
}     
istream& operator >> (istream& in, stockType& cStock)// Dawn Brownell-Adie ******

{

// Since operator>> is a friend of the stockType class, we can access StockType members directly.

       in >> cStock.ss;

       in >> cStock.op;

       in >> cStock.cp;

       in >> cStock.th;

       in >> cStock.tl;

       in >> cStock.pc;
       
       in >> cStock.pg;
       
       in >> cStock.pp;

       in >> cStock.sv;

       // cout << cStock.total_assets << endl;

          cStock.total_assets += cStock.sv*cStock.pc;

       return in;// Dawn Brownell-Adie ******************************************

}
ostream& operator<< (ostream& out,  stockType& cStock)// Dawn Brownell-Adie ******

{

        cStock.calcPercentGain();

// operator<< is a friend of the StockType class, we can access StockTypes members directly.

       out << setw(8) << left << cStock.ss << right << fixed << setprecision(2) << setw(8) <<

              cStock.op << fixed << setprecision(2) << setw(8) <<

              cStock.cp << fixed << setprecision(2) << setw(8) <<

              cStock.th << fixed << setprecision(2) << setw(8) <<

              cStock.tl << fixed << setprecision(2) << setw(8) <<

              cStock.pc << fixed << setprecision(2) << setw(8) << 
              
              cStock.pg << fixed << setprecision(2) << setw(2) << 
              
              cStock.pp << fixed << setprecision(2) << setw(12) <<

              cStock.sv << fixed << setprecision(2) <<endl;

       return out;// Dawn Brownell-Adie ****

}
 void stockType::getTotalAssets()// Dawn Brownell-Adie ****

{ 

 cout << cp * sv;// Dawn Brownell-Adie ****

}
stockType::stockType(string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double percentGain, char percent, int volume)
{
      if ("A" <= symbol && symbol < "z") 
      ss = symbol; 
      else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose && prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (-999999 <= percentGain && percentGain < 9999)
      pg = percentGain;
      else pg = 0;
      if (pp == '%')
      pp = percent;
      else pp = 0;
      if (0 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;
}
stockType::stockType() 
{ 
    ss = 'A'; 
    op = 0; 
    cp = 0;
    th = 0;
    tl = 0;
    pc = 0;
    pg = 0;
    pp = 0;
    sv = 0; 
};

 int main()
{
     string ss;
        double op, cp, th, tl, pc, pg;
        char pp;
        int sv;
    
    stockType stock;    
    
    ifstream in("Stocks.txt");  
 
    while (in >> stock)
        cout << stock << "\n";
     stock.printStock();
        cout << endl << "\n";

 system ("pause");
 return 0;    
}   
O.K., this works. Now we have to get the rest working. Here's the code that works:
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
// Kenneth Clark

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;


class stockType
{
public:
       void setStock (string, double, double, double, double, double, double, char, int);
       void getStock (string&, double&, double&, double&, double&, double&, double&, char&, int&)const;
       void printStock() const;//works
       void showPriceDiff();//works
       void calcPercentGain();//working
       void showShares(); //working
       friend istream& operator >> (istream &in, stockType &cStock);// Dawn Brownell-Adie ****
       friend ostream& operator << (ostream &out,  stockType &cStock);// Dawn Brownell-Adie ****
       void getTotalAssets();// working ****// Dawn Brownell-Adie ****
       stockType(string, double, double, double, double, double, double, char, int);
       stockType();//works

     string ss;
        double op, cp, th, tl, pc, pg;
        char pp;
        int sv;
    
        double total_assets;// Dawn Brownell-Adie ****
};
void stockType::setStock (string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double percentGain, char percent, int volume)
{//*Daniel De Gasperis***************************************************************
                                                                                  //*
      if ("A" <= symbol && symbol < "z") 
      ss = symbol; 
      else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose && prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (-999999 <= percentGain && percentGain < 9999)
      pg = percentGain;
      else pg = 0;
      if (pp == '%')
      pp = percent;
      else pp = 0;
      if (0 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;                                                                //*
                                                                                  //*
}//*Daniel De Gasperis***************************************************************

void stockType::getStock(string& symbol, double& openingPrice, double& closingPrice, 
double& todayHigh, double& todayLow, double& prevClose, double& percentGain, char& percent, int& volume) const 
{ 
     symbol = ss; openingPrice = op; closingPrice = cp; todayHigh = th;
     todayLow = tl; prevClose = pc; percentGain = pg; percent = pp; volume = sv;
}

void stockType::printStock() const 
{ 
       cout << ss << " " << op << " " << cp << " " << th << " " << tl << " " << pc << " " 
            << pg << pp << " " << sv << "\n" << endl; 
} 

void stockType::showPriceDiff()// Nichole Knowles*****************************************
{                                                                                      //*
cout << "Today's opening price was " << op << ", and the closing was " << cp << endl;  //*
cout << "Today's high price was " << th << ", and the low price was " << tl << endl;   //*
cout << "While the previous closing price was " << pc << endl;                         //*
}                                                                                      //*
void stockType::calcPercentGain()                                                    //*
{                                                                                      //*
cout << (cp-pc)/pc*100;                                                           //*
} // Nichole Knowles**********************************************************************
void stockType::showShares()
{
cout << sv;     
}     
istream& operator >> (istream& in, stockType& cStock)// Dawn Brownell-Adie ******

{

// Since operator>> is a friend of the stockType class, we can access StockType members directly.

       in >> cStock.ss;

       in >> cStock.op;

       in >> cStock.cp;

       in >> cStock.th;

       in >> cStock.tl;

       in >> cStock.pc;
       
       in >> cStock.pg;
       
       in >> cStock.pp;

       in >> cStock.sv;

       // cout << cStock.total_assets << endl;

          cStock.total_assets += cStock.sv*cStock.pc;

       return in;// Dawn Brownell-Adie ******************************************

}
ostream& operator<< (ostream& out,  stockType& cStock)// Dawn Brownell-Adie ******

{// operator<< is a friend of the StockType class, we can access StockTypes members directly.

               out << setw(8) << left <<
               
               cStock.ss << right << fixed << setprecision(2) << setw(8) <<

               cStock.op << fixed << setprecision(2) << setw(8) <<

               cStock.cp << fixed << setprecision(2) << setw(8) <<

               cStock.th << fixed << setprecision(2) << setw(8) <<

               cStock.tl << fixed << setprecision(2) << setw(8) <<

               cStock.pc << fixed << setprecision(2) << setw(8) << 
              
               cStock.pg << fixed << setprecision(2) << setw(2) << 
              
               cStock.pp << fixed << setprecision(2) << setw(12) <<

               cStock.sv << fixed << setprecision(2) <<endl;

       return out;// Dawn Brownell-Adie ****

}
 void stockType::getTotalAssets()// Dawn Brownell-Adie ****

{ 

 cout << cp * sv << endl;// Dawn Brownell-Adie ****

}
stockType::stockType(string symbol, double openingPrice, double closingPrice, 
double todayHigh, double todayLow, double prevClose, double percentGain, char percent, int volume)
{
      if ("A" <= symbol && symbol < "z") 
      ss = symbol; 
      else ss = ' '; 
      if (0 <= openingPrice && openingPrice < 9999) 
      op = openingPrice; 
      else op = 0; 
      if (0 <= closingPrice && closingPrice < 9999) 
      cp = closingPrice; 
      else cp = 0;
      if (0 <= todayHigh && todayHigh < 9999)
      th = todayHigh;
      else th = 0;
      if (0 <= todayLow && todayLow < 9999)
      tl = todayLow;
      else tl = 0;
      if (0 <= prevClose && prevClose < 9999)
      pc = prevClose;
      else pc = 0;
      if (-999999 <= percentGain && percentGain < 9999)
      pg = percentGain;
      else pg = 0;
      if (pp == '%')
      pp = percent;
      else pp = 0;
      if (0 <= volume && volume < 999999)
      sv = volume;
      else sv = 0;
}
stockType::stockType() 
{ 
    ss = 'A'; 
    op = 0; 
    cp = 0;
    th = 0;
    tl = 0;
    pc = 0;
    pg = 0;
    pp = 0;
    sv = 0; 
};

 int main()
{
     string ss;
        double op, cp, th, tl, pc, pg;
        char pp;
        int sv;
    
    stockType stock;    
    
    ifstream in("Stocks.txt");  
 
    while (in >> stock)
        cout << stock << "\n";
        stock.printStock();
        stock.getTotalAssets();
        stock.showPriceDiff();
        stock.showShares();
        stock.calcPercentGain();
        cout << endl << "\n";

 system ("pause");
 return 0;    
}   

I'm sorry if I have been misunderstood. When I said "we" I meant the other students and I. You folks here have given more help than I deserved, especially Chervil.
Thank you,
No need for apologies. I'm assuming that currently there aren't any specific questions at present.

One suggestion - this is purely a matter of style rather than getting correct results.

I noticed some code which seemed a good place to use the conditional or ternary operator ? :. It's a concise way of assigning a value and replaces an if/else statement.

An if-else like this:
1
2
3
4
    if (0 <= closingPrice && closingPrice < 9999)
        cp = closingPrice; 
    else 
        cp = 0;
can be written on a single line as follows:
 
    cp = (0 <= closingPrice && closingPrice < 9999) ? closingPrice : 0 ;


To begin with the syntax can appear puzzling, but it may be worth becoming familiar with it. See Conditional ternary operator
http://www.cplusplus.com/doc/tutorial/operators/

That is awesome! I will start on that right now. You know the project pretty well now. I think I need to get these different variables into some sort of array so that my printStock() will work on more than just the last line of the input file. I am just guessing about the array mostly because I have never used one with more than one type in it, and I have never tried to declare one as a class member before. Please excuse my ignorance this is my first real C++ class and it feels like I don't have enough background to cover the material.
One of the rather nice things about objects and classes in C++ is that if you know the syntax and way of doing something with one type (such as int), then doing more or less the same thing with a user-defined type is almost identical.

You've already seen that, for example to display an integer, you put cout << n; where n is an int. And if n was an object of type stockType the code would look exactly the same (though the result would of course be different).

I'm not sure how familiar with using arrays, if you know to declare an array of integers, then you know how to declare an array of stockTypes. If you know how to read integers from a file into an array, then you know how to do the same with stockType.

(personally I'd use a std::vector instead of an array, but if you haven't learned that yet, it doesn't matter, just use an ordinary array).

Last edited on
O.K., so brace yourself for another stupid question. If I understand, which it is doubtful, it is alright to put other data types in an array or vector that is declared as an int?
Like I can put char data in an int array that also has string and or int data in it already?
No, not really.
An array contains just a single type. It can be one of (for example) char or int or string ... but only one of those at a time.

But - a user-defined object such as stockType can itself be contained in an array. And then within each of those objects, there are the individual member variables of type string, int, double etc.

An array of 20 integers looks like this:
 
    int intarr[20];


An array of 20 stockType objects looks like this:
 
    stockType stockarr[20];


If you want to call a member function of an object in the array, you'd do it like this:
 
    stockarr[3].printStock();



Thanks again, I think you might have solved the rest of my issues. Well, with this program anyway.
Topic archived. No new replies allowed.