c++ - need help fixing minor issues in my program -
so here code:
#include <iostream> #include <string> #include <vector> using namespace std; class point { private: double px; double py; public: void setx(const double x); void sety(const double y); double getx() const; double gety() const; }; class rectangle { private: string name; point blpoint; double length, height; public: // member functions void setname(const string & inname); void setbottomleft(const double x, const double y); void setdimensions(const double inlength, const double inheight); string getname() const; point getbottomleft() const; double getlength() const; double getheight() const; double area() const; double perimeter() const; point midpoint() const; void scaleby2(); void display() const; }; // function prototypes go here: void welcome(); bool read_rec(const string promptname, const string errorinvalid, const string errorused, string & inname, vector<rectangle> & list); void read_coord(const string promptpoint, double & x, double & y); void read_length(const string promptlength, double & inlength, double & inheight); void add_rec(const string name, double x, double y, double inlength, double inheight, vector<rectangle> & list); int main() { // define local variables, e.g. vector of class rectangle rectangle rec; vector<rectangle> list; string prompt1stname = "enter name of first rectangle: "; string promptname = "enter name of next rectangle: "; string errorinvalid = "invalid input. type 'rec' following name or 'stop' if done."; string errorused = "this name being used!"; string inname; string name; // display welcome banner welcome(); /* prompt user first rectangle or 'stop' */ bool read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); // while user input invalid while (read == false) { // display "try again! " cout << "try again! " << endl; read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); } // if user input not 'stop' if (inname != "stop") { // extract rectangle name user input int = inname.length() - 4; name = inname.substr(4, a); // prompt bottom left point double x, y; string promptpoint = "enter " + name + "'s bottom left x , y coords: "; read_coord(promptpoint, x, y); // prompt length , height double inlength, inheight; string promptlength = "enter " + name + "'s length , height: "; read_length(promptlength, inlength, inheight); // add rectangle rectangle list add_rec(name, x, y, inlength, inheight, list); } /* prompt user next rectangle or 'stop' */ // while user input not 'stop' while (inname != "stop") { // display "thank you! " cout << "thank you! "; bool read = read_rec(promptname, errorinvalid, errorused, inname, list); // while user input invalid while (read == false) { // display "try again! " cout << "try again! " << endl; read = read_rec(promptname, errorinvalid, errorused, inname, list); } // if user input not 'stop' if (inname != "stop") { // extract rectangle name user input int = inname.length() - 4; name = inname.substr(4, a); // prompt bottom left point double x, y; string promptpoint = "enter " + name + "'s bottom left x , y coords: "; read_coord(promptpoint, x, y); // prompt length , height double inlength, inheight; string promptlength = "enter " + name + "'s length , height: "; read_length(promptlength, inlength, inheight); // add rectangle rectangle list add_rec(name, x, y, inlength, inheight, list); } } // if rectangle list not empty if (list.size() != 0) { // display rectangles in rectangle list int rec_num = 0; int = 1; while (i< list.size()) { rec_num++; i++; } cout << "you have " << rec_num+1 << " rectangle(s) in list: "; cout << endl; (int = 0; < list.size(); i++) { cout << "rectangle '" << list[i].getname() << "' : "; list[i].display(); list[i].scaleby2(); cout << " after scale 2: "; list[i].display(); cout << endl; } } // else else { // display no rectangles in list cout << "you have no rectangles in list." << endl; } return 0; } // function definitions go here: void welcome() { cout << "welcome! create own list of rectangles." << endl; cout << "you asked provide information each rectangle in list name." << endl; cout << "type word 'stop' rectangle name when done." << endl; cout << endl; } bool read_rec(const string promptname, const string errorinvalid, const string errorused, string & inname, vector<rectangle> & list) { cout << promptname; getline(cin, inname); if (inname == "stop") { return(true); } else if (inname.substr(0,4) != "rec ") { cout << errorinvalid; return(false); } else { int j = 0; (int = 0; < list.size(); i++) { if (inname == "rec " + list[i].getname()) { j = j+1; } } if (j == 0) { return(true); } if (j != 0) { cout << errorused; return(false); } } } void read_coord(const string promptpoint, double & x, double & y) { cout << promptpoint; cin >> x; cin >> y; } void read_length(const string promptlength, double & inlength, double & inheight) { cout << promptlength; cin >> inlength; cin >> inheight; cout << endl; while (inlength <= 0 || inheight <= 0) { cout << "make length , height positive values. try again."; cout << promptlength; cin >> inlength; cin >> inheight; cout << endl; } } void add_rec(const string name, double x, double y, double inlength, double inheight, vector<rectangle> & list) { rectangle rec; rec.setname(name); rec.setbottomleft(x, y); rec.setdimensions(inlength, inheight); list.push_back(rec); } // class member function definitinos go here: void point::setx(const double x) { px = x; } void point::sety(const double y) { py = y; } double point::getx() const { return (px); } double point::gety() const { return (py); } void rectangle::setname(const string & inname) { name = inname; } void rectangle::setbottomleft(const double x, const double y) { blpoint.setx(x); blpoint.sety(y); } void rectangle::setdimensions(const double inlength, const double inheight) { length = inlength; height = inheight; } string rectangle::getname() const { return (name); } point rectangle::getbottomleft() const { return (blpoint); } double rectangle::getlength() const { return (length); } double rectangle::getheight() const { return (height); } double rectangle::area() const { // area = length * height return(length * height); } double rectangle::perimeter() const { // perimeter = 2 * (length + height); return(2 * (length + height)); } point rectangle::midpoint() const { point midpoint; double mx = blpoint.getx() + 0.5 * length; double = blpoint.gety() + 0.5 * height; midpoint.setx(mx); midpoint.sety(my); return(midpoint); } void rectangle::scaleby2() { double midx = blpoint.getx() + 0.5 * length; double midy = blpoint.gety() + 0.5 * height; double newblpx = midx - length; double newblpy = midy - height; length = 2*length; height = 2*height; blpoint.setx(newblpx); blpoint.sety(newblpy); } void rectangle::display() const { cout << " location (" << blpoint.getx() << ", " << blpoint.gety() << "), length " << length << ", height " << height << "; area " << area() << "; perimeter " << perimeter() << ", midpoint located @ (" << midpoint().getx() << ", " << midpoint().gety() << ")" << endl; }
the problem's have program outputs "invalid input. type 'rec' following name or 'stop' if done.", , not know how change this. , when put in duplicate answer in rec fire , rec fire, rec fire being used , continue prompt rectangle instead of asking name. appreciated!!
this wrong
/* prompt user first rectangle or 'stop' */ bool read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); // while user input invalid while (read == false) { // display "try again! " cout << "try again! " << endl; bool read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); }
you have two read variables, read variable in while condition referring read variable declared first, read variable declared second never used. want this
/* prompt user first rectangle or 'stop' */ bool read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); // while user input invalid while (read == false) { // display "try again! " cout << "try again! " << endl; read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); }
now have 1 read variable. accounts second error describe think.
another way of coding this
for (;;) { bool read = read_rec(prompt1stname, errorinvalid, errorused, inname, list); if (read) break; cout << "try again! " << endl; }
in view kind of loop better because doesn't have duplicated call read_rec, style of loop mistake made impossible.
Comments
Post a Comment