C++ Class rectangle assign values - c++

I'm writing a class rectangle for this assignment Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.
I keep getting an error advising setLengthandWidth, setLength, and setWidth must return a value. I'm not sure where I'm going wrong. Any advice? Here are my 3 files.
Header file
#ifndef Rectangle_H
#define Rectangle_H
class Rectangle
{
public:
Rectangle();
Rectangle(float length);
Rectangle(float length, float width);
~Rectangle();
float setLengthAndWidth(float, float);
float setLength(float Length);
float setWidth(float Width);
float calculatePerimeter();
float calculateArea();
void printInfo();
float getLength();
float getWidth();
private:
float length;
float width;
float area;
float perimeter;
};
#endif#pragma once
main.cpp file
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
int main()
{
Rectangle objectOne;
Rectangle objectTwo(7.1, 3.2);
Rectangle objectThree(6.3);
Rectangle objectFour(200, 300);
Rectangle objectFive = objectTwo;
cout << "The first objects information is\n ";
objectOne.printInfo();
cout << "The second objects information is\n ";
objectTwo.printInfo();
cout << "The third objects information is\n ";
objectThree.printInfo();
cout << "The fourth objects information is\n ";
objectFour.printInfo();
cout << "The fifth objects information is\n ";
objectFive.printInfo();
}
member .cpp file
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
length = width = 1.0;
}
Rectangle::Rectangle(float length)
{
setLengthAndWidth(length, 1.0);
}
Rectangle::Rectangle(float length, float width)
{
setLengthAndWidth(length, width);
}
float Rectangle::setLengthAndWidth(float Len, float Wid)
{
setLength(Len);
setWidth(Wid);
}
float Rectangle::setLength(float length)
{
if (length >= 0 || length <= 20.0)
length = length;
else
length = 1.0;
}
float Rectangle::setWidth(float width)
{
if (width >= 0 || width <= 20.0)
width = width;
else
width = 1.0;
}
float Rectangle::calculatePerimeter()
{
perimeter = (length * 2) + (width * 2) ;
return perimeter;
}
float Rectangle::calculateArea()
{
area = length * width;
return area;
}
float Rectangle::getLength()
{
cout << "Please enter length" << endl;
cin >> length;
return length;
}
float Rectangle::getWidth()
{
cout << "Please enter width" << endl;
cin >> width;
return width;
}
void Rectangle::printInfo()
{
cout << "the length is " << length << endl << "the width is " << width << endl;
cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;
}
Rectangle::~Rectangle()
{
cout << "the object has gone out of scope. ";
}

In the declaration and definition of setLengthandWidth, setLength, and setWidth methods, you specify that they return a float data type :
//In header
float setLengthAndWidth(float, float);
float setLength(float Length);
float setWidth(float Width);
//In CPP file
float Rectangle::setLengthAndWidth(float Len, float Wid)
{
setLength(Len);
setWidth(Wid);
}
float Rectangle::setLength(float length)
{
if (length >= 0 || length <= 20.0)
length = length;
else
length = 1.0;
}
float Rectangle::setWidth(float width)
{
if (width >= 0 || width <= 20.0)
width = width;
else
width = 1.0;
}
If you want to not get that error, change the data type from float to void in the method declaration and definitions, like this :
//In header
void setLengthAndWidth(float, float);
void setLength(float Length);
void setWidth(float Width);
//In CPP file
void Rectangle::setLengthAndWidth(float Len, float Wid)
{
setLength(Len);
setWidth(Wid);
}
void Rectangle::setLength(float length)
{
if (length >= 0 || length <= 20.0)
length = length;
else
length = 1.0;
}
void Rectangle::setWidth(float width)
{
if (width >= 0 || width <= 20.0)
width = width;
else
width = 1.0;
}

Why does your getLength and getWidth methods modify values? Generally accessor methods are only supposed return the appropriate fields.
HatsuPointerKun is correct though.
The methods have a return type of float but fail to return any value.
Either have them return a float number (most likely whatever you set them to) or change the return type to void.

Related

Class inheritance with vector of parents having plural child elements

I'm trying to build a gamengine, and I need to sort characters and rooftiles based on the y-coordinate of their feet before drawing them, so that folk/tiles infront are drawn ontop of folk/tiles behind them. I've gotten it to work with just characters, but I need rooftiles as well.
the classes are both children of renderable, and share some data. The classes are forward-declared, and there are global lists g_entities, g_tiles, and g_renderable.
class renderable {
public:
float x;
float y;
float width;
float height;
SDL_Surface* image;
SDL_Texture* texture;
void render(SDL_Renderer * renderer, camera fcamera) {
rect obj(floor((x -fcamera.x)* fcamera.zoom), floor((y-fcamera.y)* fcamera.zoom), floor(width *fcamera.zoom), floor(height * fcamera.zoom));
rect cam(0, 0, fcamera.width, fcamera.height);
if(RectOverlap(obj, cam)) {
SDL_Rect dstrect = { (x -fcamera.x)* fcamera.zoom, (y-fcamera.y)* fcamera.zoom, width *fcamera.zoom, height * fcamera.zoom};
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
}
}
};
class entity: public renderable {
public:
float x;
float y;
float xagil;
float yagil;
//... other stuff, including image and texture
void render(SDL_Renderer * renderer, camera fcamera);
};
class tile: public renderable {
public:
int x;
int y;
int z = 0; //represents layer. 0 default
float width;
float height;
//... other stuff, including image and texture
void render(SDL_Renderer * renderer, camera fcamera);
};
The list is created when the map is loaded with
for (int i = 0; i < g_entities.size(); i++) {
g_renderable.push_back(g_entities[i]);
}
for (int i = 0; i < g_tiles.size(); i++) {
if(g_tiles[i]->z ==2) {
g_renderable.push_back(g_tiles[i]);
g_tiles.erase(g_tiles.begin() + i);
i--;
}
}
And then in main I planned to sort the g_renderable array instead of the g_entities array and have everything snug, but instead I get a SIGFAULT. I tracked down the problem with GDB and it turns out that g_renderable[] isnt getting informed with the attributes of g_entities[] (and probly not g_tiles[] either).
This is what I mean:
g_renderable.clear();
g_renderable.reserve(g_entities.size() + g_tiles.size()); // preallocate memory
cout << "g_entities.size" <<g_entities.size() << endl;
cout << "g_entities.size" <<g_entities.size() << endl;
cout << "g_entities height " << g_entities[0]->height << endl; //couts 122
for (int i = 0; i < g_entities.size(); i++) {
g_renderable.push_back(g_entities[i]);
}
cout << "g_renderable height " << g_renderable[0]->height << endl; //couts 0
for (int i = 0; i < g_tiles.size(); i++) {
if(g_tiles[i]->z ==2) {
g_renderable.push_back(g_tiles[i]);
g_tiles.erase(g_tiles.begin() + i);
i--;
}
}
Why doesnt my vector return the member data as expected?

std::logic_error when trying to return a std::string

I have written some code using a class that will display the measurements of a box. I am doing so by having the output in a toString() method and it appears to be working but when i run the program I get the following error:
Height: 1 Width: 1 Depth: 1terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Then the program chrashes. Also I've noticed that the program doesn't display the volume after the 3 dimensions.
Here is the code:
#include <iostream>
#include <iomanip> // for output formatting
#include <stdexcept> // for out_of_range
#include <sstream> // for stringstream
#include <string>
#include <cstdlib> // for system()
using namespace std;
class Box
{
public:
// Constructors
Box(double height=1, double width=1, double depth=1);
// Mutators
void setHeight(double height);
void setWidth(double width);
void setDepth(double depth);
// Accessors
double getHeight() const {return (boxHeight);};
double getWidth() const {return (boxWidth);};
double getDepth() const {return (boxDepth);};
double getVolume() const;
string toString() ;
private:
double boxHeight;
double boxWidth;
double boxDepth;
double boxVolume;
};
int main()
{
cout << "\nBox Mesurement!";
cout << "\n===============";
cout << endl;
Box boxDem(true);
// WHERE THE STRING IS DISPLAYED
cout << "\n" << boxDem.toString();
cout<< endl;
cout << "\n" << boxDem.getVolume();
return 0;
}
Box::Box(double height, double width, double depth)
{
setHeight(height);
setWidth(width);
setDepth(depth);
}
void Box::setHeight(double height)
{
const double MIN = 0.01;
if (height > 0 && height < MIN)
{
height = 0.01;
boxHeight = height;
}
else if (height < 0)
{
height *= -1;
boxHeight = height;
}
else
{
boxHeight = height;
}
}
void Box::setWidth(double width)
{
const double MIN = 0.01;
if (width > 0 && width < MIN)
{
width = 0.01;
boxWidth = width;
}
else if (width < 0)
{
width *= -1;
boxWidth = width;
}
else
{
boxWidth = width;
}
}
void Box::setDepth(double depth)
{
const double MIN = 0.01;
if (depth > 0 && depth < MIN)
{
depth = 0.01;
boxDepth = depth;
}
else if (depth < 0)
{
depth *= -1;
boxDepth = depth;
}
else
{
boxDepth = depth;
}
}
double Box::getVolume() const
{
double volume = 0.0;
volume = getHeight() * getHeight() *getDepth();
return volume;
}
// WHERE THE PROBLEM IS
string Box::toString()
{
cout << "Height: " << getHeight() << " Width: " << getWidth() << " Depth: " << getDepth();
return 0;
}
cout is meant to output stuff to the command line, but you are writing a function that's supposed to return a string, that makes little sense.
ostringstream is a neat class that allows you to build strings using the same mechanisms as cout, try this:
string Box::toString()
{
std::ostringstream result;
result << "Height: " << getHeight() << " Width: " << getWidth() << " Depth: " << getDepth();
return result.str();
}

Returning a boolean value from a Circle Class C++

I am trying to return a boolean value using a C++ class. It needs to be able to check whether circle A is the same size as circle B using an overload operator > which I have added as a public member in the class. In my int main it always seems to return false even when the circles are the same size.
Thanks, in advance.
Circle class:
#include <iostream>
#include <cmath>
using namespace std;
//creating a constant pi that can't be changed
const double pi = 3.14159265;
class Circle
{
//defining the private memeber variables
private:
double radius, xpos, ypos;
//defining the public member variables
public:
//creating a constructor that takes all of the variables
Circle(double r, double xposition, double yposition) {
radius = r;
xpos = xposition;
ypos = yposition;
}
//creating a constructor that takes just the radius
Circle(double r) {
radius = r;
xpos = 0;
ypos = 0;
}
//creating a contructor that initialised everything to 0
Circle() {
radius = 0;
xpos = 0;
ypos = 0;
}
//defining the functions for radius, X-position, Y-position and area
double getRadius() {return radius;}
double getX() {return xpos;}
double getY() {return ypos;}
double getArea() {return pi*radius*radius;}
//creating an overaload operator + to add the various properties of a circle together
Circle operator+(Circle C) {
radius = sqrt(this->getRadius()*this->getRadius() + C.getRadius()*C.getRadius()); //calculates the radius from the area
xpos = (this->getX() + C.getX()) / 2.; //calculating the half way x position
ypos = (this->getY() + C.getY()) / 2.; //calculating the half way y position
return Circle(radius, xpos, ypos);
}
//created an overload operator << that outputs information about the circle in a consistent manor
friend ostream& operator<<(ostream& os, Circle C) {
return os << "radius = " << C.getRadius() << " at (x,y) = (" << C.getX() << "," << C.getY() << ")";
}
bool operator>(Circle C) {
if (this->getRadius() > C.getRadius()) {
return true;
}
else {
return false;
}
}
};
Int main ()
#include "Circle.hpp"
using namespace std;
int main()
{
//defining the circles A and B
Circle A(4.0,2.0,1.0);
cout << "Circle A: " << A << endl;
Circle B(4.0,5.0,6.0);
cout << "Circle B: " << B << endl;
//Adds A and B using the overload operator +
Circle C = A + B;
//Outputs the formatted text using the overload operator <<
cout << "Circle C: " << C << endl;
bool test;
Circle D(4.0,2.0,1.0);
if (A > D) {
test = false;
}
else if (D > A) {
test = false;
}
else {
test = true;
}
cout << boolalpha << test << endl;
return 0;
}
A.r is 4.0. D.r is also 4.0. Neither D > A nor A > D is true. > checks for real greater than. If you want to have greater-or-equal use >= instead.
When you do the following you are changing the value of A's radius
Circle C = A + B
"In my int main it always seems to return false even when the circles are the same size"
It will surely return false when circle are of same size as your condition is:-
if ( this->getRadius() > C.getRadius() )
return true;
everything other than this would return false. If you want to return true when your circle are of same size then make it:-
if ( this->getRadius() < C.getRadius() )
return false;
else
return true;
EDITED IN RESPONSE TO COMMENT:-
Then probably you can use enum if want to to test three different scenarios:-
if ( this->getRadius() > C.getRadius() )
return ENUM_GREATER;
else if ( this->getRadius() == C.getRadius() )
return ENUM_EQUAL;
return ENUM_LESSER;
Your operator+ modifies the left-hand operand. So, this line modifies A :
Circle C = A + B;
(including the radius member). So, by the time you compare A to D, A's radius is no longer 4.0.
The operator+ can be modified as follows to fix this :
Circle operator+(const Circle& C) const {
double r = sqrt(this->getRadius()*this->getRadius() + C.getRadius()*C.getRadius()); //calculates the radius from the area
double x = (this->getX() + C.getX()) / 2.; //calculating the half way x position
double y = (this->getY() + C.getY()) / 2.; //calculating the half way y position
return Circle(r, x, y);
}

value being changed to random number on function call?

#include <iostream>
#include "Shapes.h"
int main()
{
//variables
int height = 0;
int width = 0;
Rectangle rect = Rectangle();
Triangle tran = Triangle();
Square sqar = Square();
std::cout << "What is the width of the shape? ";
std::cin >> width;
std::cout << "What is the height of the shape?";
std::cin >> height;
rect.set_lengths(width, height);
std::cout << "If the shape is a triangle, the area is " << tran.area() << "." << std::endl;
std::cout << "If the shape is a rectangle, the area is " << rect.area() << "." << std::endl;
std::cout << "If the shape is a square, the area is " << sqar.areaByWidth() << " by the width value," << std::endl;
std::cout << "and " << sqar.areaByHeight() << " by the height value." << std::endl;
system("pause");
}
Header file:
//Our base class
class Shape
{
protected:
int width, height, shapes = 0;
public:
void set_lengths(int width, int height)
{
width = width; height = height;
}
};
//Rectangle is a shape
class Rectangle : public Shape
{
public:
Rectangle()
{
std::cout << "Created a rectangle!\n";
shapes = shapes + 1;
}
~Rectangle()
{
shapes = shapes - 1;
}
int area()
{
return width * height;
}
};
//Triangle is a shape
class Triangle : public Shape
{
public:
Triangle()
{
shapes = shapes + 1;
std::cout << "Created a triangle!\n";
}
~Triangle()
{
shapes = shapes - 1;
}
int area()
{
return width * height / 2;
}
};
//Square is a shape
class Square : public Shape
{
public:
Square()
{
shapes = shapes + 1;
std::cout << "Created a square!";
}
~Square()
{
shapes = shapes - 1;
}
int areaByWidth()
{
return width * width;
}
int areaByHeight()
{
return height * height;
}
};
When I set the values, it works fine (shows the correct value in visual studio debugger), but when I call area() it brings back -846388729 or something similiar? Why is the value being reset? I have been banging my head against a wall for hours on this. Seems like a common problem to noobies like myself but I'm not understanding the other solutions on here :(
The function set_lengths did not set the member variable correctly, just set the value back to the function arguments.
change
void set_lengths(int width, int height)
{
width = width; height = height;
}
to
void set_lengths(int width, int height)
{
this->width = width; this->height = height;
}
Or change the name of the member variables for a good habit:
int width_, height_, shapes_;
void set_lengths(int width, int height)
{
width_ = width;
height_ = height;
}
Becuase width and height are not initilized.
Change the line to:
int i = 0, j = 0, k = 0;
Also, you're only setting the dimension of the rect object, not the triangle or the square.

No matching function for call to class

/*
* File: ShapeTwoD.h
* Author: Administrator
*
* Created on October 30, 2012, 12:05 AM
*/
#ifndef SHAPETWOD_H
#define SHAPETWOD_H
#include <string>
#include <math.h>
using namespace std;
struct Point {
int x, y;
};
class ShapeTwoD {
public:
ShapeTwoD(string shapename, bool containsWS);
string getName();
bool getContainsWarpSpace();
string toString();
virtual double computeArea();
virtual bool isPointInShape(Point P, Point* V, int n);
virtual bool isPointOnShape(Point A, Point B, Point C, int slope, int intercept, int left, int top, int right, int bottom, int dx, int dy);
void setName(string shapename);
void setContainsWarpSpace(bool containsWS);
private:
string name;
bool containsWarpSpace;
};
class Cross : public ShapeTwoD {
public:
Cross(string shapename = "Cross", bool containsWS, int vertices = 12, Point ordinates[]):ShapeTwoD(shapename, containsWS){}
int getVert();
void setVert(int vertices);
Point getOrd();
void setOrd(Point ordinates[]);
virtual double computeArea(Point A[], int vertices);
private:
int vert;
Point ord[];
};
class Rectangle : public ShapeTwoD {
public:
Rectangle(string shapename = "Rectangle", bool containsWS, int vertices = 4, Point ordinates[]):ShapeTwoD(shapename, containsWS){}
int getVert();
void setVert(int vertices);
Point getOrd();
void setOrd(Point ordinates[]);
virtual double computeArea(Point A, Point B);
private:
int vert;
Point ord[];
};
class Square : public ShapeTwoD {
public:
Square(string shapename = "Square", bool containsWS, int vertices = 4, Point ordinates[]):ShapeTwoD(shapename, containsWS){}
int getVert();
void setVert(int vertices);
Point getOrd();
void setOrd(Point ordinates[]);
virtual double computeArea(Point A, Point B);
private:
int vert;
Point ord[];
};
#endif /* SHAPETWOD_H */
/*
* File: ShapeTwoD.cpp
* Author: Administrator
*
* Created on October 30, 2012, 12:05 AM
*/
#include "ShapeTwoD.h"
#include <sstream>
ShapeTwoD::ShapeTwoD(string shapename, bool containsWS) {
name = shapename;
containsWarpSpace = containsWS;
}
string ShapeTwoD::getName() {
return name;
}
void ShapeTwoD::setName(string shapename) {
name = shapename;
};
bool ShapeTwoD::getContainsWarpSpace() {
return containsWarpSpace;
}
void ShapeTwoD::setContainsWarpSpace(bool containsWS) {
containsWarpSpace = containsWS;
}
string cvtBool(bool b) {
stringstream ss;
ss << b;
return ss.str();
}
string ShapeTwoD::toString() {
return "Name:\t" + name + "\nSpecial Type:\t" + cvtBool(containsWarpSpace) + "\n";
}
// Formulas gotten from http://www.mathopenref.com/coordpolygonarea.html
// http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm
int pointsInShape;
int pointsOnShape;
double ShapeTwoD::computeArea() {
// Based on Pick's Thorem
double area = pointsInShape + (pointsOnShape / 2) - 1;
return area;
}
float isLeft(Point P0, Point P1, Point P2) {
return ((P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y));
}
bool ShapeTwoD::isPointInShape(Point P, Point* V, int n) {
// Input: P = a point,
// V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
int wn = 0; // the winding number counter
// loop through all edges of the polygon
for (int i = 0; i < n; i++) { // edge from V[i] to V[i+1]
if (V[i].y <= P.y) { // start y <= P.y
if (V[i + 1].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + 1], P) > 0) // P left of edge
++wn; // have a valid up intersect
} else { // start y > P.y (no test needed)
if (V[i + 1].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + 1], P) < 0) // P right of edge
--wn; // have a valid down intersect
}
}
pointsInShape += wn;
return true;
}
bool ShapeTwoD::isPointOnShape(Point A, Point B, Point C, int slope, int intercept, int left, int top, int right, int bottom, int dx, int dy) {
// Linear equation
dx = B.x - A.x;
dy = B.y - A.y;
slope = dy / dx;
intercept = y1 - slope * A.x;
if (A.x < B.x) {
left = A.x;
right = B.x;
} else {
left = B.x;
right = A.x;
}
if (A.y < B.y) {
top = A.y;
bottom = B.y;
} else {
top = B.y;
bottom = A.y;
}
if (slope * C.x + intercept > (C.y - 0.01) && slope * C.x + intercept < (C.y + 0.01)) {
if (C.x >= left && C.x <= right && C.y >= top && C.y <= bottom) {
pointsOnShape++;
return true;
}
}
}
Cross::Cross(string shapename, bool containsWS, int vertices = 12, Point ordinates[]):ShapeTwoD(shapename, containsWS) {
vert = vertices;
ord[] = ordinates[];
}
int Cross::getVert() {
return vert;
}
void Cross::setVert(int vertices) {
vert = vertices;
}
Point Cross::getOrd() {
return ord[];
}
void Cross::setOrd(Point ordinates[]) {
ord[] = ordinates[];
}
double Cross::computeArea(Point A[], int vertices) {
/* If you know the coordinates of the vertices of a polygon, this algorithm can be used to find the area.
* Parameters
* X, Y Arrays of the x and y coordinates of the vertices, traced in a clockwise direction, starting at any vertex. If you trace them counterclockwise, the result will be correct but have a negative sign.
* numPoints The number of vertices
* Returns the area of the polygon
*/
double area = 0;
int j = vertices - 1; // The last vertex is the 'previous' one to the first
for (int i = 0; i < vertices; i++) {
area = (area + (A[j].x + A[i].x) * (A[j].y - A[i].y))/2;
j = i;
}
return area;
}
Rectangle::Rectangle(string shapename, bool containsWS, int vertices = 4, Point ordinates[]):ShapeTwoD(shapename, containsWS) {
vert = vertices;
ord[] = ordinates[];
}
int Rectangle::getVert() {
return vert;
}
void Rectangle::setVert(int vertices) {
vert = vertices;
}
void Rectangle::getOrd() {
return ord[];
}
void Rectangle::setOrd(Point ordinates[]) {
ord[] = ordinates[];
}
double Rectangle::computeArea(Point A, Point B, Point C) {
double length = sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2));
double width = sqrt(pow((B.x - C.x), 2) + pow((B.y - C.y), 2));
double area = length * width;
return area;
}
Square::Square(string shapename, bool containsWS, int vertices, Point ordinates[]):ShapeTwoD(shapename, containsWS) {
vert = vertices;
ord[] = ordinates[];
}
int Square::getVert() {
return vert;
}
void Square::setVert(int vertices) {
vert = vertices;
}
void Square::getOrd() {
return ord[];
}
void Square::setOrd(Point ordinates[]) {
ord[] = ordinates[];
}
double Square::computeArea(Point A, Point B) {
double length = sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2));
double area = pow(length, 2);
return area;
}
/*
* File: Assn2.cpp
* Author: Administrator
*
* Created on October 29, 2012, 11:58 PM
*/
#include "ShapeTwoD.h"
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
// Global declarations
void menu(), option1(), option2(), option3(), option4();
int choice, vert;
string shape, special;
double area;
bool containsWS;
vector<ShapeTwoD> stdv;
int main() {
cout << "Welcome to Assn2 program!\n\n";
// When user enters 5 as input, the program quits
while (choice != 5) {
menu();
/* switch evaluates expression and checks if it is equivalent to constant1,
* if it is, it executes group of statements 1 until it finds the break statement.
* When it finds this break statement the program jumps to the end of the switch selective structure.
* If expression was not equal to constant1 it will be checked against constant2.
* If it is equal to this, it will execute group of statements 2 until a break keyword is found,
* and then will jump to the end of the switch selective structure.
*/
switch (choice) {
case 1:
option1();
break;
case 2:
option2();
break;
case 3:
option3();
break;
case 4:
option4();
break;
}
}
}
void menu() {
cout << "1) Input sensor data\n";
cout << "2) Compute area (for all records)\n";
cout << "3) Print shapes report\n";
cout << "4) Sort shape data\n";
cout << "5) Quit\n\n";
cout << "Please enter your choice: ";
cin >> choice;
cout << "\n";
}
void option1() {
cout << "[ Input sensor data ]\n";
cout << "Please enter name of Shape (Cross, Rectangle or Square): ";
cin >> shape;
cout << "Please enter Special type (NS or WS): ";
cin >> special;
if (special == "WS") {
containsWS = true;
}
else {
containsWS = false;
}
if (shape == "Cross") {
vert = 12;
for (int v = 0; v < vert; v++) {
Point ordinates[v];
cout << "Please enter x-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].x;
cout << "Please enter y-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].y;
Cross cross(shape, containsWS, vert, ordinates[v]);
stdv.push_back(cross);
}
}
if (shape == "Rectangle") {
vert = 4;
for (int v = 0; v < vert; v++) {
Point ordinates[v];
cout << "Please enter x-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].x;
cout << "Please enter y-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].y;
Rectangle rectangle(shape, containsWS, vert, ordinates[v]);
stdv.push_back(rectangle);
}
}
else {
shape = "Square";
vert = 4;
for (int v = 0; v < vert; v++) {
Point ordinates[v];
cout << "Please enter x-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].x;
cout << "Please enter y-ordinate of pt. " << (v+1) << ":";
cin >> ordinates[v].y;
Square square(shape, containsWS, vert, ordinates[v]);
stdv.push_back(square);
}
}
cout << "Record successfully stored. Going back to main menu";
}
void option2() {
if (stdv.size() != 0) {
for (int count = 0; count < stdv.size(); count++) {
area = stdv.at(count).computeArea();
//Debugging purpose
cout << (count+1) << ")" << stdv.at(count).getName() << "\t" << area;
}
cout << "Computation completed! (" << stdv.size() << " records were updated)\n\n";
}
}
Here I'm trying to insert an array of the struct Point into my class constructors (Cross, Rectangle, Square). But it returned me some errors which I don't really get.
Assn2.cpp:113: error: no matching function for call to
`Cross::Cross(std::string&, bool&, int&, Point&)'
ShapeTwoD.h:41: note: candidates are: Cross::Cross(const Cross&)
ShapeTwoD.h:43: note: Cross::Cross(std::string, bool,
int, Point*)
Assn2.cpp:136: error: no matching function for call to
`Rectangle::Rectangle(std::string&, bool&, int&, Point&)
ShapeTwoD.h:56: note: candidates are: Rectangle::Rectangle(const
Rectangle&)
ShapeTwoD.h:58: note: Rectangle::Rectangle(std::string, bool, int,
Point*)
your function declaration won't even compile, the parameter with default value can't be in the front of argument without default value:
Cross(string shapename = "Cross", bool containsWS, int vertices = 12, Point ordinates[])
Rectangle(string shapename = "Rectangle", bool containsWS, int vertices = 4, Point ordinates[]):ShapeTwoD(shapename, containsWS){}
Square(string shapename = "Square", bool containsWS, int vertices = 4, Point ordinates[]):ShapeTwoD(shapename, containsWS){}
you either make all parameters having all default values or change the order, for example below is valid function declaration:
Cross(bool containsWS, Point ordinates[], string shapename = "Cross", int vertices = 12)