c++ - Calling void function problems -
hello have problems calling void initialize() function in code. supposed array looks this
//cccccccccccccccccccccccccc cxxxxxxxxxxxxxxxxxxxxxxxxc cxxxxxxxxxxxxxxxxxxxxxxxxc cxxxxxxxxxxxxxxxxxxxxxxxxc cxxxxxxxxxxxxxxxxxxxxxxxxc cxxxxhhhhhhhxxxxxxxxxxxxxc cxxxxhhhhhhhxxxxxxxxxxxxxc cxxxxhhhhhhhxxxxxxxxxxxxxc cxxxxhhhhhhhxxxxxxxxxxxxxc cxxxxxxxxxxxxxxxxxxxxxxxxc cccccccccccccccccccccccccc//
where array defined in initialize() , variables come fin , steampipe , gridpt classes. code giving me first set of answers before array when gets array symbol '?' it's been designated in class gridpt. think way calling void function wrong or there wrong passing variables? tried taking out const still same. here code .h file
#include<iostream> #include<istream> #include<cmath> #include<cstdlib> using namespace std; const int maxfinheight = 100; const int maxfinwidth = 100; class steampipe { private: int height, width; double steamtemp; public: steampipe (int h = 0, int w = 0, double st = 0.0) { height = h; width = w; steamtemp = st; } // access function definitions int getwidth()const{return width;}; int getheight()const{return height;}; double getsteamtemp()const{return steamtemp;}; void setwidth(int dummysteamwidth) {width = dummysteamwidth;}; void setheight(int dummysteamheight){height = dummysteamheight;}; void setsteamtemp(double dummysteamtemp){steamtemp = dummysteamtemp;}; // access function definitions friend istream& operator>>(istream& , steampipe& ); // must define friend ostream& operator<<(ostream& , const steampipe& ); // must define }; class gridpt { private: double temperature; char symbol; public: gridpt(double t = 0.0, char s = '?') { temperature = t; symbol = s; } // access function definitions double gettemperature()const {return temperature;}; char getsymbol() const {return symbol;}; void settemperature (double t=0) {t=temperature;} void setsymbol (char s='?'){s=symbol;} }; class fin { private: int width, height; //width , height of fin int pipelocationx, pipelocationy; //grid location of lower left corner of steampipe double boundarytemp; //temperature of fin surroundings steampipe pipe; //steampipe object - composition gridpt gridarray[maxfinheight][maxfinwidth]; // array of gridpts - composition public: int getwidth() {return width;} int getheight() {return height;} int getpipelocationx(){return pipelocationx;} int getpipelocationy(){return pipelocationx;} double get_boundarytemp(){return boundarytemp;} void setwidth (int w){w=width;} void setheight (int h){h=height;} friend istream& operator>>(istream& , fin& ); // must define friend ostream& operator<<(ostream& , const fin& ); // must define void initialize(); // must define }; void fin::initialize() { int j(0) ,i(0); ( = 0; < height; i++) { ( j = 0; j < width; j++) { if ( j == pipelocationx && == pipelocationy && < pipe.getheight() + pipelocationy && j < pipe.getwidth() + pipelocationx) { gridarray [j][i].settemperature(pipe.getsteamtemp()); gridarray [j][i].setsymbol('h'); } else if (j == (width -1) || == (height -1) || j == 0 || == 0) { gridarray [j][i].settemperature(boundarytemp); gridarray [j][i].setsymbol('c'); } else { gridarray [j][i].settemperature((pipe.getsteamtemp() + boundarytemp)/2); gridarray [j][i].setsymbol('x'); } } } } istream &operator >> (istream & in, fin& f) { int ws, hs; double ts; char comma; cout << "enter height of fin (integer) >>> "; in >> f.height; cout << "enter width of fin (integer) >>> " ; in >> f.width; cout << "enter height of streampipe (integer) >>> "; in >>hs; f.pipe.setheight(hs); cout << "enter width of steampipe (integer) >>> "; in >> ws; f.pipe.setwidth(ws); cout << "enter coordinates of lower left corner of steampipe (x,y) >>> "; in >> f.pipelocationx >> comma >> f.pipelocationy; cout << "enter steam temperature (floating point) >>> "; in >> ts; f.pipe.setsteamtemp(ts); cout << "enter temperature around fin (floating point) >>> "; in >> f.boundarytemp; return in; } ostream &operator << (ostream &stream, const fin& t) { int = 0; int j = 0; stream << "the width of fin " << t.width << endl; stream << "the height of fin " << t.height << endl; stream << "the outside temperature " << t.boundarytemp << endl; stream << "the lower left corner of steam pipe @ " << t.pipelocationx << "," << t.pipelocationy << endl; stream << "the steampipe width " << t.pipe.getwidth() << endl; stream << "the steampipe height " << t.pipe.getheight() << endl; stream << "the temperature of steam " << t.pipe.getsteamtemp() << endl; ( = 0; < t.height; i++) { ( j = 0; j < t.width; j++) { if (j == t.pipelocationx && == t.pipelocationy && < t.pipe.getheight() + t.pipelocationy && j < t.pipe.getwidth() + t.pipelocationx) { t.gridarray [j][i].gettemperature(); stream<<t.gridarray [j][i].getsymbol(); } else if (j == (t.width -1) || == (t.height -1) || j == 0 || == 0) { t.gridarray [j][i].gettemperature(); stream<<t.gridarray [j][i].getsymbol(); } else { t.gridarray [j][i].gettemperature(); stream<<t.gridarray [j][i].getsymbol(); } }stream<<endl; } int coorx(0),coory(0);char comma; stream << "enter coordinates of gridpoint of interest (x,y) >>> " << endl; cin>>coorx>>comma>>coory; stream<<"the temperature @ location ("<<coorx<<","<<coory<<") " << t.gridarray << endl; return stream; }
and driver file cpp is:
#include "pipe.h" int main() { fin one; cin >> one; one.initialize(); cout << one; return 0; }
any suggestions appreciated. thank you
in code following wrong:
void settemperature (double t=0) {t=temperature;} void setsymbol (char s='?'){s=symbol;}
it must be:
void settemperature (double t=0) {temperature = t;} void setsymbol (char s='?') { symbol = s;}
as aside: not habit place function definitions in header (unless it's template), rather put them in source-file declarations in header.
in code use t.gridarray
in output-stream directly:
stream<<t.gridarray;
not work. not function, or type ostream understand. use , j variables have declared , step through each element of array print them, same when initialize array.
Comments
Post a Comment