C means line skip Right now I have a program that does ever
C++
....... means line skip
Right now I have a program that does everything i want it to with the correct inputs, but I need some help fixing my code in order for the program to output exactly what I need with weird inputs.
This is how the code should act.
Welcome! Create your own list of rectangles.
You will be asked to provide information about each rectangle in your list by name.
Type the word \'stop\' for the rectangle name when you are done.
.........
Enter the name of the first rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: rec one
Enter one\'s bottom left x and y coords: 1 1
Enter one\'s length and height: 1 1
............
Thank you! Enter the name of the next rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: rec two
and the program continues like normal from here
//////////////
but with my code, here is what is happening with the same input of \"a\".
/////////////
Welcome! Create your own list of rectangles.
You will be asked to provide information about each rectangle in your list by name.
Type the word \'stop\' for the rectangle name when you are done.
..........
Enter the name of the first rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: a
Invalid Input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: rec one
Enter one\'s bottom left x and y coords: 1 1
Enter one\'s length and height: 1 1
..........
Thank you! Enter the name of the next rectangle: a
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! a
.........
Try again! Enter the name of the next rectangle: Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! a
..........
Try again! Enter the name of the next rectangle: Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again!
........
// Notice how it skips lines now, and I don\'t know how to make it say Enter the name of the next triangle for all the rest besides the first rectangle. even if I type in a name starting with rec after the failed attempts, the program wont continue.
.......
Here is my 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 scaleBy4();
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
void display_banner();
bool read_valid_name(const string & prompt, const string & errMessage,
const string & errDuplicate,
string & name, const vector<Rectangle> & nameList);
void read_point(const string & prompt, double & x, double & y);
void read_dimensions(const string & prompt, double & length, double & height);
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height,
vector<Rectangle> & list);
void display_rectangles(vector<Rectangle> & list);
// MAIN FUNCTION GO HERE
int main()
{
display_banner();
cout << endl;
string promptA1=\"Enter the name of the first rectangle: \";
string promptA2=\"Enter the name of the next rectangle: \";
string comment;
string promptB=\"\'s bottom left x and y coords: \";
string promptC=\"\'s length and height: \";
string errName=\"Invalid input. Type \'rec\' following by the name or \'stop\' if done.\";
string errDuplicate=\"Invalid input. There is already a rectangle by that name. Enter another name\";
string name;
bool good;
double x,y,length,height;
vector<Rectangle> R;
do{
good=read_valid_name(promptA1,errName,errDuplicate,name,R);
}while(good!=true);
while(name!=\"stop\"){
read_point(\"Enter \"+name+promptB,x,y);
read_dimensions(\"Enter \"+name+promptC,length,height);
add_rec(name,x,y,length,height,R);
comment=\"\ Thank you! \";
do{
cin.clear();cin.ignore();
good=read_valid_name(comment+promptA2,errName,errDuplicate,name,R);
comment=\"\ Try again! \";
}while(good!=true);
}
display_rectangles(R);
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void display_banner(){
cout<<\"Welcome! Create your own list of rectangles.\ \"
<<\"You will be asked to provide information about each rectangle in your list by name.\"
<<\"\ Type the word \'stop\' for the rectangle name when you are done.\ \";
return;
}
bool read_valid_name(const string & prompt, const string & errMessage,const string & errDuplicate,
string & name, const vector<Rectangle> & nameList)
{
cout<<prompt;
getline(cin,name,\'\ \');
if(name==\"stop\"){
return true;
}
if(name.substr(0,4)!=\"rec \"){//if it doesnt start with \'rec \', show error message
cout<<errMessage << endl << \"Try again! \";
return false;
}
if(isalpha(name[4])==0){
cout<<errMessage<<endl;
return false;
}
name=name.substr(4,name.length());
/*std::vector<Rectangle>::iterator itr;
for ( itr = nameList.begin(); itr != nameList.end(); ++itr ){//check for duplicates
*/
if(nameList.size()>0){
for(int i=0;nameList[i].getName()!=nameList.back().getName();i++){
//for(int i=0;i<nameList.size();i++){
//if(itr->getName()==name){
if(nameList[i].getName()==name){
cout<<errDuplicate<<endl;
return false;
}
}
}
return true;
}
void read_point(const string & prompt, double & x, double & y){
cout<<prompt;
cin>>x>>y;
return;
}
void read_dimensions(const string & prompt, double & length, double & height){
cout<<prompt;
cin>>length;
cin>>height;
return;
}
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height, vector<Rectangle> & list){
Rectangle temp;
temp.setName(name);
temp.setDimensions(length,height);
temp.setBottomLeft(x,y);
list.push_back(temp);
return;
}
void display_rectangles(vector<Rectangle> & list){
if (list.size() >0)
{
cout<<\"\ You have \"<<list.size()<<\" rectangle(s) in your list:\ \ \";
}
else {
cout << \"\ You have no rectangles in your list.\";
}
for(int i=0;i<list.size();i++){
//itr->display();
cout<<\"Rectangle \'\"<<list[i].getName()<<\"\': \";
list[i].display();
cout<<\"\ After scale by 4:\ \";
list[i].scaleBy4();
list[i].display();
cout<<endl;
}
}
// CLASS MEMBER FUNCTION DEFINITIONS GO HERE:
///Point
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;
}
///Rectangle
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)
{
if(inLength>0)
length=inLength;
else{
cout<<\"\ Length must be greater than 0. Length set to 1\ \";
length=1;
}
if(inHeight>0)
height=inHeight;
else{
cout<<\"\ Height must be greater than 0. Height set to 1\ \";
height=1;
}
return;
}
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
{
return length*height;
}
double Rectangle::perimeter() const
{
return 2*(length+height);
}
Point Rectangle::midPoint() const
{
Point temp;
temp.setX(blPoint.getX()+(length/2.0));
temp.setY(blPoint.getY()+(height/2.0));
return temp;
}
void Rectangle::scaleBy4()
{
blPoint.setX(blPoint.getX()-(length/(2.0/3.0)));
blPoint.setY(blPoint.getY()-(height/(2.0/3.0)));
//multuply lenght by 4 and heigh by 4
length=4*length;
height=4*height;
}
void Rectangle::display() const
{
Point temp=midPoint();
cout<<\"Location is (\"<<blPoint.getX()<<\", \"<<blPoint.getY()<<\")\"
<<\", length is \"<<length<<\", height is \"<<height
<<\"; Area is \"<<area()<<\", perimeter is \"<<perimeter()
<<\", midpoint is located at (\"
<< temp.getX() <<
\", \" << temp.getY() << \")\" <<endl;
}
Solution
// C++ 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 scaleBy4();
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
void display_banner();
bool read_valid_name(const string & prompt, const string & errMessage,
const string & errDuplicate,
string & name, const vector<Rectangle> & nameList);
void read_point(const string & prompt, double & x, double & y);
void read_dimensions(const string & prompt, double & length, double & height);
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height,
vector<Rectangle> & list);
void display_rectangles(vector<Rectangle> & list);
// MAIN FUNCTION GO HERE
int main()
{
display_banner();
cout << endl;
string promptA1=\"Enter the name of the first rectangle: \";
string promptA2=\"Enter the name of the next rectangle: \";
string comment;
string promptB=\"\'s bottom left x and y coords: \";
string promptC=\"\'s length and height: \";
string errName=\"Invalid input. Type \'rec\' following by the name or \'stop\' if done.\";
string errDuplicate=\"Invalid input. There is already a rectangle by that name. Enter another name\";
string name;
bool good;
double x,y,length,height;
vector<Rectangle> R;
do{
good=read_valid_name(promptA1,errName,errDuplicate,name,R);
}while(good!=true);
while(name!=\"stop\"){
read_point(\"Enter \"+name+promptB,x,y);
read_dimensions(\"Enter \"+name+promptC,length,height);
add_rec(name,x,y,length,height,R);
comment=\"\ Thank you! \";
int flag = 1;
do{
// skip the input for the first time when entering into this do while loop
if(flag == 1)
{
cin.clear();
cin.ignore();
}
good=read_valid_name(comment+promptA2,errName,errDuplicate,name,R);
flag++;
comment=\"\";
}while(good!=true);
}
display_rectangles(R);
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void display_banner(){
cout<<\"Welcome! Create your own list of rectangles.\ \"
<<\"You will be asked to provide information about each rectangle in your list by name.\"
<<\"\ Type the word \'stop\' for the rectangle name when you are done.\ \";
return;
}
bool read_valid_name(const string & prompt, const string & errMessage,const string & errDuplicate,
string & name, const vector<Rectangle> & nameList)
{
cout<<prompt;
getline(cin,name,\'\ \');
if(name==\"stop\"){
return true;
}
if(name.substr(0,4)!=\"rec \"){//if it doesnt start with \'rec \', show error message
cout<<errMessage << endl << \"Try again! \";
return false;
}
if(isalpha(name[4])==0){
cout<<errMessage<<endl;
return false;
}
name=name.substr(4,name.length());
/*std::vector<Rectangle>::iterator itr;
for ( itr = nameList.begin(); itr != nameList.end(); ++itr ){//check for duplicates
*/
if(nameList.size()>0){
for(int i=0;nameList[i].getName()!=nameList.back().getName();i++){
//for(int i=0;i<nameList.size();i++){
//if(itr->getName()==name){
if(nameList[i].getName()==name){
cout<<errDuplicate<<endl;
return false;
}
}
}
return true;
}
void read_point(const string & prompt, double & x, double & y){
cout<<prompt;
cin>>x>>y;
return;
}
void read_dimensions(const string & prompt, double & length, double & height){
cout<<prompt;
cin>>length;
cin>>height;
return;
}
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height, vector<Rectangle> & list){
Rectangle temp;
temp.setName(name);
temp.setDimensions(length,height);
temp.setBottomLeft(x,y);
list.push_back(temp);
return;
}
void display_rectangles(vector<Rectangle> & list){
if (list.size() >0)
{
cout<<\"\ You have \"<<list.size()<<\" rectangle(s) in your list:\ \ \";
}
else {
cout << \"\ You have no rectangles in your list.\";
}
for(int i=0;i<list.size();i++){
//itr->display();
cout<<\"Rectangle \'\"<<list[i].getName()<<\"\': \";
list[i].display();
cout<<\"\ After scale by 4:\ \";
list[i].scaleBy4();
list[i].display();
cout<<endl;
}
}
// CLASS MEMBER FUNCTION DEFINITIONS GO HERE:
///Point
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;
}
///Rectangle
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)
{
if(inLength>0)
length=inLength;
else{
cout<<\"\ Length must be greater than 0. Length set to 1\ \";
length=1;
}
if(inHeight>0)
height=inHeight;
else{
cout<<\"\ Height must be greater than 0. Height set to 1\ \";
height=1;
}
return;
}
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
{
return length*height;
}
double Rectangle::perimeter() const
{
return 2*(length+height);
}
Point Rectangle::midPoint() const
{
Point temp;
temp.setX(blPoint.getX()+(length/2.0));
temp.setY(blPoint.getY()+(height/2.0));
return temp;
}
void Rectangle::scaleBy4()
{
blPoint.setX(blPoint.getX()-(length/(2.0/3.0)));
blPoint.setY(blPoint.getY()-(height/(2.0/3.0)));
//multuply lenght by 4 and heigh by 4
length=4*length;
height=4*height;
}
void Rectangle::display() const
{
Point temp=midPoint();
cout<<\"Location is (\"<<blPoint.getX()<<\", \"<<blPoint.getY()<<\")\"<<\", length is \"<<length<<\", height is \"<<height<<\"; Area is \"<<area()<<\", perimeter is \"<<perimeter()<<\", midpoint is located at (\"<< temp.getX() <<\", \" << temp.getY() << \")\" <<endl;
}
/*
output:
Welcome! Create your own list of rectangles.
You will be asked to provide information about each rectangle in your list by name.
Type the word \'stop\' for the rectangle name when you are done.
Enter the name of the first rectangle: a
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: a
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the first rectangle: rec one
Enter one\'s bottom left x and y coords: 1 1
Enter one\'s length and height: 2 1
Thank you! Enter the name of the next rectangle: a
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: a
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: rec two
Enter two\'s bottom left x and y coords: 2 1
Enter two\'s length and height: 5 1
Thank you! Enter the name of the next rectangle: p
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: rec three
Enter three\'s bottom left x and y coords: 9 1
Enter three\'s length and height: 21 2
Thank you! Enter the name of the next rectangle: l
Invalid input. Type \'rec\' following by the name or \'stop\' if done.
Try again! Enter the name of the next rectangle: rec stop
You have 3 rectangle(s) in your list:
Rectangle \'one\': Location is (1, 1), length is 2, height is 1; Area is 2, perimeter is 6, midpoint is located at (2, 1.5)
After scale by 4:
Location is (-2, -0.5), length is 8, height is 4; Area is 32, perimeter is 24, midpoint is located at (2, 1.5)
Rectangle \'two\': Location is (2, 1), length is 5, height is 1; Area is 5, perimeter is 12, midpoint is located at (4.5, 1.5)
After scale by 4:
Location is (-5.5, -0.5), length is 20, height is 4; Area is 80, perimeter is 48, midpoint is located at (4.5, 1.5)
Rectangle \'three\': Location is (9, 1), length is 21, height is 2; Area is 42, perimeter is 46, midpoint is located at (19.5, 2)
After scale by 4:
Location is (-22.5, -2), length is 84, height is 8; Area is 672, perimeter is 184, midpoint is located at (19.5, 2)
*/
















