Having trouble moving class to seperate .cpp and .h file. - c++

I created a basic consol program that creates a box of the users desired height and width. I wanted to learn how classes worked so I just used one file. Now i'm trying to properly put the class into a .h and .cpp file. Can someone point out what I'm doing wrong and how I should fix it?
Original Code with just a main.cpp:
#include "stdafx.h"
#include <iostream>
using namespace std;
class BoxClass {
//prv variables
unsigned short int width;
int height, i;
float space_Value;
float height_Count = 1;
bool error = false;
//prv functions
void Print_Rectangle(int x, int y) {
//calc
space_Value = (3 * x) - 4;
//draw top of box
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < y; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < space_Value; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
cout << ":";
for (width = 1; width < space_Value; width += 1) {
cout << ".";
}
cout << ":\n";
}
public:
//function shows area of individual spaces
float Rectangle_Area() {
if (error == false) {
return (height_Count - .5)*(space_Value - 1);
}
else {
return 0;
}
}
//function shows area of individual spaces
// constructor
BoxClass(int x, int y, int amount) {
if (x <= 41) {
for (i = 1; i <= amount; i += 1) {
Print_Rectangle(x, y);
}
}
else {
error = true;
cout << "Error - width must be below 42!\n";
}
};
};
int main() {
//variable declaration/definition
int width_Var;
int height_Var;
int number_of_Boxes;
//object declaration/Body
cout << "Enter width of rectangle/box\nWidth = ";
cin >> width_Var;
cout << "Enter height of rectangle/box\nHeight = ";
cin >> height_Var;
cout << "How many rectangles/boxes do you want?\n";
cin >> number_of_Boxes;
BoxClass box1(width_Var, height_Var, number_of_Boxes);
cout <<"Box Area = "<<box1.Rectangle_Area() << endl;
//exit
cout << "\n\n\n\n\n";
system("pause");
return 0;
}
New Code with main.cpp, BoxClass.h, Boxclass.cpp:
main.cpp:
#include "BoxClass.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
//variable declaration/definition
int width_Var;
int height_Var;
int number_of_Boxes;
//object declaration/Body
cout << "Enter width of rectangle/box\nWidth = ";
cin >> width_Var;
cout << "Enter height of rectangle/box\nHeight = ";
cin >> height_Var;
cout << "How many rectangles/boxes do you want?\n";
cin >> number_of_Boxes;
BoxClass box1(width_Var, height_Var, number_of_Boxes);
cout <<"Box Area = "<<box1.Rectangle_Area() << endl;
//exit
cout << "\n\n\n\n\n";
system("pause");
return 0;
}
BoxClass.h:
#ifndef BOXCLASS_H
#define BOXCLASS_H
class BoxClass {
//prv variables
unsigned short int width;
int height, i;
float space_Value;
float height_Count = 1;
bool error = false;
//prv functions
void Print_Rectangle(int x, int y);
public:
//function shows area of individual spaces
float Rectangle_Area();
//function shows area of individual spaces
// constructor
BoxClass(int x, int y, int amount);
};
#endif
BoxClass.cpp:
#include "BoxClass.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
BoxClass::Print_Rectangle(int x, int y) {
//calc
space_Value = (3 * x) - 4;
//draw top of box
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < y; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < space_Value; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
cout << ":";
for (width = 1; width < space_Value; width += 1) {
cout << ".";
}
cout << ":\n";
}
//function shows area of individual spaces
BoxClass::Rectangle_Area() {
if (error == false) {
return (height_Count - .5)*(space_Value - 1);
}
else {
return 0;
}
}
// constructor
BoxClass::BoxClass(int x, int y, int amount) {
if (x <= 41) {
for (i = 1; i <= amount; i += 1) {
Print_Rectangle(x, y);
}
}
else {
error = true;
cout << "Error - width must be below 42!\n";
}
};

You forgot about type specifier so you should change this in your BoxClass.cpp:
BoxClass::Print_Rectangle(int x, int y)
...
BoxClass::Rectangle_Area()
to this:
void BoxClass::Print_Rectangle(int x, int y)
...
float BoxClass::Rectangle_Area()
Also a bit aside of topic, thing that I can suggest you for future is to use #pragma once instead include guards.

You need a constructor, which is where you'll set the values of height_Count and error. You should probably also define a destructor, even though at this point there is probably nothing to do there. It's just a good idea. According to Scott Meyers in Effective C++, you should also define a copy constructor and a copy assignment operator.
This is in addition to the type-specifier suggestion already posted.

Related

Printing triangle without nested conditionals

#include<iostream>
using namespace std;
int main() {
int height, star{ 0 };
cout << "Enter height of triangle";
cin >> height;
for(int i=1; i<=height; i++)
{
if(star<i)
{
cout << "*";
++star;
continue;
}
cout << endl;
star = 0;
}
}
This is printing stars in a line
I want to print one star in 1st line then 2 in second line and so forth.
Example:
*
**
***
****
Image:
You should be able to simply do this:
#include <iostream>
using namespace std;
int main()
{
int height;
cout << "Enter height of triangle";
cin >> height;
for(int i=1; i<=height; i++)
{
cout << string(i, '*') << endl;
}
}
You could do this. This uses the length of the string directly instead of a separate counter i.
#include <iostream>
#include <string>
int main() {
int height;
std::cout << "Enter height of triangle";
std::cin >> height;
for(std::string stars = "*"; stars.length() <= height; stars += "*")
{
std::cout << stars << std::endl;
}
return 0;
}
I don't really understand the reason for your question, but hey, why not:
#include <iostream>
#include <string>
int main() {
int height;
std::cout << "Enter height of triangle";
std::cin >> height;
std::string stars = "*";
for(int i=1; i<=height; i++)
{
std::cout << stars << std::endl;
stars += "*";
}
return 0;
}
No loops:
void stars(int height) {
if (!height) return;
stars(height-1);
std::cout << std::string(height, '*') << '\n';
}
int main() {
int height;
std::cout << "Enter height of triangle";
std::cin >> height;
stars(height);
}
Or to not use the loop in std::string(height, '*') as idz suggested:
void stars(int height, std::string &s) {
if (!height) return;
std::cout << s << '\n';
stars(height-1, s+='*');
}
int main() {
int height;
std::cout << "Enter height of triangle\n";
std::cin >> height;
std::string s = "*";
stars(height, s);
}
Here is a C inspired solution. Don't write C++ like this. It's essentially just to show another way to do it than the already posted solutions:
#include <iostream>
int main() {
int height;
std::cout << "Enter height of triangle";
std::cin >> height;
char *arr = new char[height];
std::fill(arr, arr+height+1, '\0');
for(int i=0; i<height; i++) {
arr[i] = '*';
std::cout << arr << std::endl;
}
delete arr;
}
Not necessarily a good solution, but it's an option:
#include <iomanip>
#include <iostream>
int main() {
int height;
std::cout << "Enter height of triangle";
std::cin >> height;
std::cout << std::setfill('*'); // fill the default blank spaces with * instead
++height;
for(int i = 2; i <= height; ++i) {
std::cout << std::setw(i) << '\n'; // fill width i until the newline
}
}
first of all it's not a half triangle it's a triangle or for you we can count that a half rectangle
but leaving that one here what you are looking for is most likely this way
#include <bits/stdc++.h>
for making it easier
using namespace std ;
/*you can always use std::"command" when you ain't using absolute standard name spaces but leaving that one there*/
int main ()
{
for ( string s = "Symbol" ; s.length() <= h ;s += "Symbol" )
{
cout << s << "\n" ;
}
return 0 ;
}

How would I create a class with a variable name in the class name?

So, this is my code:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle {
public:
double width;
double height;
rectangle(double, double);
double area() { return (width*height);}
};
rectangle::rectangle(double a, double b) {
width = a;
height = b;
}
int main() {
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++) {
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect/*integer value of rectCount at current time*/(rectWidth, rectHeight);
}
return 0;
}
As the comment section says, I would like to create a rectangle called rect, with a suffix which is the current value of the integer rectCount. How would I do this? Thanks in advance!
This idiom is never useful in C++. The correct approach is to store your rectangles in a container like std::vector which will grow and shrink automatically to meet your needs. Then, you can loop through the vector and not worry about how many elements are actually in there.
std::vector<rectangle> rects(rectNum);
for (int counter = 0; counter < rectNum; counter++) {
/* .. */
rects.emplace_back(rectWidth, rectHeight);
}
The loop is not needed actually. std::vector's constructor will take care of it for you.
std::vector<rectangle> rects(rectNum, {rectWidth, rectHeight});
You need to include <vector> in order to use std::vector.
variable names must be defined at compile time so what you are trying to do will not work.
Dynamically setting class names is not possible in C++. However, a static variable can be used to store a running count of rectangles:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle
{
public:
double width;
double height;
static int rectCount; //declare static variable
rectangle(double, double);
double area() { return (width*height);}
};
int rectangle::rectCount=0; //initialize static variable
rectangle::rectangle(double a, double b)
{
width = a;
height = b;
rectCount++;
}
int main()
{
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++)
{
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect=rectangle(rectWidth, rectHeight);
cout<<"rectCount value: "<<rectCount;
}
return 0;
}

Exit with value 1 C++(Vectors) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm a Java programmer trying to get used to C++ and so far NetBeans is not helping me, I'm having Run Failed(exit value 1, total time: 10ms); I've checked some solutions for problems like mine, but It's not working, so far I'm able to understand that usually this error appears with arrays with non-allocated memory, that is not my case(I Guess).
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class Rover {
private:
string name;
int position[2];
string direction;
int speed;
public:
Rover();
Rover(string name, int x, int y, string direction, int speed);
void getRoverData();
void setName(string name);
string getName();
void setPosition(int x, int y);
int getPosition();
void setDirection(string diretction);
string getDirection();
void setSpeed(int speed);
int getSpeed();
};
Rover::Rover() {
this->position[0] = 0;
this->position[1] = 0;
this->direction = "North";
}
Rover::Rover(string name, int x, int y, string direction, int speed) {
this->name = name;
this->position[0] = x;
this->position[1] = y;
this->direction = direction;
this->speed = speed;
}
void Rover::setDirection(string direction) {
this->direction = direction;
}
void Rover::setName(string name) {
this->name = name;
}
void Rover::setPosition(int x, int y) {
this->position[0] = x;
this->position[1] = y;
}
void Rover::setSpeed(int speed) {
this->speed = speed;
}
string Rover::getDirection() {
return this->direction;
}
string Rover::getName() {
return this->name;
}
int Rover::getPosition() {
//ToDo -> send array, not value at index, how I can do it?=]
return this->position[0];
}
int Rover::getSpeed() {
return this->speed;
}
void Rover::getRoverData() {
cout << "Rover name is " << this->name << endl;
cout << "The Rover position is(X,Y) " << this->position[0] << "," << this->position[1] << endl;
cout << "Rover is going to " << this->direction << endl;
cout << "Rover speed is(M/s) " << this->speed << endl;
}
int main(int argc, char** argv) {
//vector<Rover> *vectorOfRover = new vector<Rover>();
vector<Rover> vectorOfRover;
int i = 0;
while (sizeof (vectorOfRover) <= 5) {
string tempName;
int tempX;
int tempY;
string tempDirection;
int tempSpeed;
cout << "Enter Rover name " << endl;
cin >> tempName;
cout << "Enter X position " << endl;
cin >>tempX;
cout << "Enter Y position " << endl;
cin >> tempY;
cout << "Enter the Rover direction " << endl;
cin >> tempDirection;
cout << "Enter the Rover speed " << endl;
cin >> tempSpeed;
Rover r1 = Rover(tempName, tempX, tempY, tempDirection, tempSpeed);
vectorOfRover.push_back(r1);
i++;
}
for (int j = 0; j <= i; j++) {
Rover r = vectorOfRover[j];
r.getRoverData();
}
return 0;
}
This line doesn't make sense:
while (sizeof (vectorOfRover) <= 5) {
sizeof operator is evaluated at compile time, so it doesn't depend on the actual number of items in the vector. Most likely sizeof(std::vector) is greater than 5 so you skip the loop completely. What you meant is probably
while (vectorOfRover.size() <= 5) {
And then you go out of bounds here:
for (int j = 0; j <= i; j++) {
Change it to
for (int j = 0; j < i; j++) {
I think part of the problem is calling sizeof on a vector. That just does not make any sense: sizeof is a compile time operator that returns the size in memory of a certain object, thus it will never return the number of elements in a collection, e.g., see this.

An error I don't understand [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm getting this error when I compile, but I'm not sure how to fix it. I see that it has something to do with trying to use ofstream outFile; to write to a file. I've diddled with it, but I'm not sure what the problem is.
1>Rectangle.obj : error LNK2001: unresolved external symbol "class std::basic_ofstream<char,struct std::char_traits<char> > outFile" (?outFile##3V?$basic_ofstream#DU?$char_traits#D#std###std##A)
1>E:\Hw11_overload_Hw8\Debug\Rectangle.exe : fatal error LNK1120: 1 unresolved externals
This is my header file.
#ifndef Rectangle_h
#define Rectangle_h
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Rectangle
{
friend istream &operator>> (istream &input, Rectangle &grid);
friend ostream &operator<< (ostream &output, Rectangle grid);
private:
int i, j, h, x1, x2, y1, y2, perimeter, area, width, height;
char inner, outer;
string name;
public:
Rectangle();
void printGrid();
void setX1(int x11);
int getX1();
void setX2(int x21);
int getX2();
void setY1(int y11);
int getY1();
void setY2(int y21);
int getY2();
void setInner(char inner1);
char getInner();
void setOuter(char outer1);
char getOuter();
void findPerimeter();
void findArea();
void findWidth();
void findHeight();
void setName(string name1);
string getName();
int Menu();
};
#endif
And here is my source file.
#include "Rectangle.h"
extern ofstream outFile;
Rectangle::Rectangle()
{
x1 = 0;
x2 = 1;
y1 = 0;
y2 = 1;
}
void Rectangle::setX1(int x11)
{
x1 = x11;
}
int Rectangle::getX1()
{
return x1;
}
void Rectangle::setX2(int x21)
{
x2 = x21;
}
int Rectangle::getX2()
{
return x2;
}
void Rectangle::setY1(int y11)
{
y1 = y11;
}
int Rectangle::getY1()
{
return y1;
}
void Rectangle::setY2(int y21)
{
y2 = y21;
}
int Rectangle::getY2()
{
return y2;
}
void Rectangle::findPerimeter()
{
perimeter = ((x2 - x1) + (y2 - y1)) * 2;
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}
void Rectangle::findArea()
{
area = (x2 - x1) * (y2 - y1);
cout << "The area of the rectangle is: " << area << endl;
}
void Rectangle::findWidth()
{
width = x2 - x1;
cout << "The width of the rectangle is: " << width << endl;
}
void Rectangle::findHeight()
{
height = y2 - y1;
cout << "The height of the rectangle is: " << height << endl;
}
void Rectangle::setInner(char inner1)
{
inner = inner1;
}
char Rectangle::getInner()
{
return inner;
}
void Rectangle::setOuter(char outer1)
{
outer = outer1;
}
char Rectangle::getOuter()
{
return outer;
}
void Rectangle::setName(string name1)
{
name = name1;
}
string Rectangle::getName()
{
return name;
}
void Rectangle::printGrid()
{
int numrows = 25;
int numcols = 25;
int current_row = numrows; // starting row number
int current_col = 1; // starting col number
char output = '.';
for(i = 0; i < numrows; i++) // prints 25 rows of 25 dots
{
cout << current_row << '\t'; // Print out current row number
outFile << current_row << '\t';
//This is out loop for each ROW
//Print out dots in each row OR stuff for the rectangle
for(j = 1; j <= numcols; j++) {
output = '.'; // Initialize output with our default value of "."
if ((current_col >= x1) && (current_col <= x2) && (current_row >= y1) && (current_row <= y2)) {
output = outer;
}
if ((current_col > x1) && (current_col < x2) && (current_row > y1) && (current_row < y2)) {
output = inner;
}
cout << output << " "; // output our "output" value and a space
outFile << output << " ";
current_col++; //Increment current column, because this is the end of this column loop iteration
} // Close column loop
cout << endl; //...and a new line
outFile << endl;
current_col = 1; // reset column count for next iteration
current_row--; // decrement current row number
} // Close Row loop
//output column labels across bottom line
cout << '\t';
outFile << '\t';
// put 1 -> 25 across the bottom
for (i = 1; i <= 25; i++)
{
if(i < 10)
{
cout << i << " ";
outFile << i << " ";
}
if(i > 9)
{
cout << i << " ";
outFile << i << " ";
}
}
// Spit out a couple of blank lines
cout << endl << endl;
outFile << endl << endl;
}
int Rectangle::Menu()
{
ifstream inFile;
int choice;
cout << "1. Enter information about a rectangle. " << endl;
cout << "2. Search for a rectangle. " << endl;
cout << "3. Print the perimeter and the area of the rectangle. " << endl;
cout << "4. Print the width and height of the rectangle. " << endl;
cout << "5. Draw a particular rectangle. " << endl;
cout << "6. Quit. " << endl;
cout << "Enter your choice. " << endl;
inFile >> choice;
cout << endl << endl;
return choice;
}
istream &operator>> (istream &input, Rectangle &grid)
{
ifstream inFile;
ofstream outFile;
Rectangle rect[10];
int x11, x21, y11, y21, choice, numRectangles = 0, i = 0;
char inner1, outer1;
string name1;
inFile.open ("rectangle.in");
outFile.open ("rectangle.out");
if (!inFile)
{
cout << "ERROR: inFile does not exist. " << endl;
system("pause");
}
if (!outFile)
{
cout << "ERROR: outFile does not exist. " << endl;
system("pause");
}
choice = rect[i].Menu();
while(choice >= 1 && choice <= 6)
{
if(choice == 1)
{ do
{
cout << "Enter x1, x2, y1, and y2 such that x1 < x2 and y1 < y2: " << endl;
inFile >> x11;
inFile >> x21;
inFile >> y11;
inFile >> y21;
cout << "Enter a character for the interior of the rectangle. \n";
inFile >> inner1;
cout << "Enter a character for the exterior of the rectangle. \n";
inFile >> outer1;
cout << "Enter a name for the rectangle. \n";
inFile >> name1;
rect[numRectangles].setX1(x11);
rect[numRectangles].setX2(x21);
rect[numRectangles].setY1(y11);
rect[numRectangles].setY2(y21);
rect[numRectangles].setInner(inner1);
rect[numRectangles].setOuter(outer1);
rect[numRectangles].setName(name1);
numRectangles++;
}
while(x11 >= x21 || y11 >= y21);
cout << endl << "Rectangle accepted. \n" << endl;
}
if (choice == 2)
{
cout << "To search for a rectangle, enter the name of the rectangle. " << endl;
cin >> name1;
for(i = 0; i < numRectangles; i++)
{
if(name1 == rect[i].getName())
{
rect[i].findPerimeter();
rect[i].findArea();
rect[i].findWidth();
rect[i].findHeight();
rect[i].printGrid();
system("pause");
}
}
if(name1 != rect[i].getName())
{
cout << "ERROR! Rectangle doesn't exist!" << endl << endl;
system("pause");
}
}
if(choice == 3)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].findPerimeter();
cout << endl;
rect[i].findArea();
cout << endl;
}
}
if(choice == 4)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].findWidth();
cout << endl;
rect[i].findHeight();
cout << endl;
}
}
if(choice == 5)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].printGrid();
cout << endl;
}
}
if(choice == 6)
{
cout << "Bye bye!" << endl;
system("pause");
}
choice = rect[i].Menu();
return input;
} // End of while loop
inFile.close();
outFile.close();
}
ostream &operator<< (ostream &output, Rectangle grid)
{
return output;
}
And here is my main function
#include "Rectangle.h"
int main()
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}
Problems:
You have declared that outFile will have external linkage. std::ofstream has no default constructor.
std::ofstream has a non-default constructor and takes two parameters.. The first is the location of the file and the second (which is default btw) is how you plan to open/write it.
Thus you can fix this error by replacing extern ofstream outFile; with:
ofstream outFile("output.txt"); within Rectangle.cpp
There is no need to declare it as extern within the .cpp file as far as I am aware.
Next, istream& operator>> (istream& input, Rectangle& grid) is returning NOTHING..
It should be returning input before the function finishes.
Finally, you are missing a curly bracket after main's () brackets.
You main.cpp should look like:
#include "Rectangle.h"
int main()
{
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}
Would it really kill you to follow convention and format your code?
If done correctly, your code would look like:
Rectangle.h:
#ifndef Rectangle_h
#define Rectangle_h
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Rectangle
{
private:
friend istream& operator>> (istream& input, Rectangle& grid);
friend ostream& operator<< (ostream& output, Rectangle grid);
int i, j, h, x1, x2, y1, y2, perimeter, area, width, height;
char inner, outer;
string name;
public:
Rectangle();
void printGrid();
void setX1(int x11);
int getX1();
void setX2(int x21);
int getX2();
void setY1(int y11);
int getY1();
void setY2(int y21);
int getY2();
void setInner(char inner1);
char getInner();
void setOuter(char outer1);
char getOuter();
void findPerimeter();
void findArea();
void findWidth();
void findHeight();
void setName(string name1);
string getName();
int Menu();
};
#endif
Rectangle.cpp:
#include "Rectangle.h"
ofstream outFile("output.txt");
Rectangle::Rectangle()
{
x1 = 0;
x2 = 1;
y1 = 0;
y2 = 1;
}
void Rectangle::setX1(int x11)
{
x1 = x11;
}
int Rectangle::getX1()
{
return x1;
}
void Rectangle::setX2(int x21)
{
x2 = x21;
}
int Rectangle::getX2()
{
return x2;
}
void Rectangle::setY1(int y11)
{
y1 = y11;
}
int Rectangle::getY1()
{
return y1;
}
void Rectangle::setY2(int y21)
{
y2 = y21;
}
int Rectangle::getY2()
{
return y2;
}
void Rectangle::findPerimeter()
{
perimeter = ((x2 - x1) + (y2 - y1)) * 2;
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}
void Rectangle::findArea()
{
area = (x2 - x1) * (y2 - y1);
cout << "The area of the rectangle is: " << area << endl;
}
void Rectangle::findWidth()
{
width = x2 - x1;
cout << "The width of the rectangle is: " << width << endl;
}
void Rectangle::findHeight()
{
height = y2 - y1;
cout << "The height of the rectangle is: " << height << endl;
}
void Rectangle::setInner(char inner1)
{
inner = inner1;
}
char Rectangle::getInner()
{
return inner;
}
void Rectangle::setOuter(char outer1)
{
outer = outer1;
}
char Rectangle::getOuter()
{
return outer;
}
void Rectangle::setName(string name1)
{
name = name1;
}
string Rectangle::getName()
{
return name;
}
void Rectangle::printGrid()
{
int numrows = 25;
int numcols = 25;
int current_row = numrows; // starting row number
int current_col = 1; // starting col number
char output = '.';
for(i = 0; i < numrows; i++) // prints 25 rows of 25 dots
{
cout << current_row << '\t'; // Print out current row number
outFile << current_row << '\t';
//This is out loop for each ROW
//Print out dots in each row OR stuff for the rectangle
for(j = 1; j <= numcols; j++)
{
output = '.'; // Initialize output with our default value of "."
if ((current_col >= x1) && (current_col <= x2) && (current_row >= y1) && (current_row <= y2))
{
output = outer;
}
if ((current_col > x1) && (current_col < x2) && (current_row > y1) && (current_row < y2))
{
output = inner;
}
cout << output << " "; // output our "output" value and a space
outFile << output << " ";
current_col++; //Increment current column, because this is the end of this column loop iteration
} // Close column loop
cout << endl; //...and a new line
outFile << endl;
current_col = 1; // reset column count for next iteration
current_row--; // decrement current row number
} // Close Row loop
//output column labels across bottom line
cout << '\t';
outFile << '\t';
// put 1 -> 25 across the bottom
for (i = 1; i <= 25; i++)
{
if(i < 10)
{
cout << i << " ";
outFile << i << " ";
}
if(i > 9)
{
cout << i << " ";
outFile << i << " ";
}
}
// Spit out a couple of blank lines
cout << endl << endl;
outFile << endl << endl;
}
int Rectangle::Menu()
{
ifstream inFile;
int choice;
cout << "1. Enter information about a rectangle. " << endl;
cout << "2. Search for a rectangle. " << endl;
cout << "3. Print the perimeter and the area of the rectangle. " << endl;
cout << "4. Print the width and height of the rectangle. " << endl;
cout << "5. Draw a particular rectangle. " << endl;
cout << "6. Quit. " << endl;
cout << "Enter your choice. " << endl;
inFile >> choice;
cout << endl << endl;
return choice;
}
istream& operator>> (istream& input, Rectangle& grid)
{
ifstream inFile;
ofstream outFile;
Rectangle rect[10];
int x11, x21, y11, y21, choice, numRectangles = 0, i = 0;
char inner1, outer1;
string name1;
inFile.open ("rectangle.in");
outFile.open ("rectangle.out");
if (!inFile)
{
cout << "ERROR: inFile does not exist. " << endl;
system("pause");
}
if (!outFile)
{
cout << "ERROR: outFile does not exist. " << endl;
system("pause");
}
choice = rect[i].Menu();
while(choice >= 1 && choice <= 6)
{
if(choice == 1)
{
do
{
cout << "Enter x1, x2, y1, and y2 such that x1 < x2 and y1 < y2: " << endl;
inFile >> x11;
inFile >> x21;
inFile >> y11;
inFile >> y21;
cout << "Enter a character for the interior of the rectangle. \n";
inFile >> inner1;
cout << "Enter a character for the exterior of the rectangle. \n";
inFile >> outer1;
cout << "Enter a name for the rectangle. \n";
inFile >> name1;
rect[numRectangles].setX1(x11);
rect[numRectangles].setX2(x21);
rect[numRectangles].setY1(y11);
rect[numRectangles].setY2(y21);
rect[numRectangles].setInner(inner1);
rect[numRectangles].setOuter(outer1);
rect[numRectangles].setName(name1);
numRectangles++;
}
while(x11 >= x21 || y11 >= y21);
cout << endl << "Rectangle accepted. \n" << endl;
}
if (choice == 2)
{
cout << "To search for a rectangle, enter the name of the rectangle. " << endl;
cin >> name1;
for(i = 0; i < numRectangles; i++)
{
if(name1 == rect[i].getName())
{
rect[i].findPerimeter();
rect[i].findArea();
rect[i].findWidth();
rect[i].findHeight();
rect[i].printGrid();
system("pause");
}
}
if(name1 != rect[i].getName())
{
cout << "ERROR! Rectangle doesn't exist!" << endl << endl;
system("pause");
}
}
if(choice == 3)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].findPerimeter();
cout << endl;
rect[i].findArea();
cout << endl;
}
}
if(choice == 4)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].findWidth();
cout << endl;
rect[i].findHeight();
cout << endl;
}
}
if(choice == 5)
{
for(i = 0; i < numRectangles; i++)
{
rect[i].printGrid();
cout << endl;
}
}
if(choice == 6)
{
cout << "Bye bye!" << endl;
system("pause");
}
choice = rect[i].Menu();
return input;
} // End of while loop
inFile.close();
outFile.close();
return input;
}
ostream& operator<< (ostream& output, Rectangle grid)
{
return output;
}
main.cpp:
#include "Rectangle.h"
int main()
{
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}
I did not check anything else other than getting it to compile. That means I am not sure if you have any bugs preventing it from running correctly. I also would advice you to avoid using using namespace std; in header files.

C++ enum type not initializing properly

I am having trouble with my enum type in my code. I have a maze that I load and loop through with for loops, and if the char symbol is equal to '*' I want to set my 2D array enum to "Wall" and to "Clear" is the symbol is whitespace. Here is my code..
Header file containing enum initialization:
#ifndef TYPE_H
#define TYPE_H
struct coordinate
{
int row;
int col;
};
enum SquareType {Wall, Clear, Visited, Path};
#endif
Header File declaring the 2D array for the enum:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#include "type.h"
const int MAX = 50;
#ifndef MazeClass_H
#define MazeClass_H
class MazeClass
{
private:
SquareType Maze[MAX][MAX];
coordinate Entrance, Exit;
int height, width;
public:
MazeClass();
void ReadMaze(ifstream&);
void DisplayMaze();
coordinate GetEntrance();
coordinate GetExit();
void MarkVisited(coordinate);
void MarkPath(coordinate);
bool IsWall(int x, int y);
bool IsClear(int x, int y);
bool IsPath(int x, int y);
bool IsVisited(coordinate);
bool IsExit(int x, int y);
bool IsInMaze(int x, int y);
};
#endif
My implementation file: Notice the function to Read Maze and the function to Display maze, as this is my problem area..
#include "MazeClass.h"
#include <iostream>
#include <cstdlib>
MazeClass::MazeClass()
{
}
void MazeClass::ReadMaze(ifstream& myIn)
{
int x, y;
char ch;
myIn >> x;
myIn >> y;
height = x;
width = y;
myIn >> x;
myIn >> y;
Entrance.row = x;
Entrance.col = y;
myIn >> x;
myIn >> y;
Exit.row = x;
Exit.col = y;
myIn.ignore(100, '\n');
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
myIn.get(ch);
if(ch == ' ')
Maze[i][k] == Clear;
else
Maze[i][k] == Wall;
cout << ch;
}
cout << endl;
}
}
void MazeClass::DisplayMaze()
{
char ch;
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
if(Maze[i][k] == Wall)
{
ch = '*';
cout << ch;
}
else if(Maze[i][k] == Clear)
{
ch = ' ';
cout << ch;
}
}
cout << endl;
}
}
coordinate MazeClass::GetEntrance()
{
return Entrance;
}
coordinate MazeClass::GetExit()
{
return Exit;
}
bool MazeClass::IsWall(int x, int y)
{
if(Maze[x][y] == Wall)
return true;
else
return false;
}
bool MazeClass::IsClear(int x, int y)
{
if(Maze[x][y] == Clear)
return true;
else
return false;
}
bool MazeClass::IsExit(int x, int y)
{
if(x == Exit.row && y == Exit.col)
return true;
else
return false;
}
bool MazeClass::IsInMaze(int x, int y)
{
cout << "Height: " << height << " " << "Width: " << width << endl;
if(x < 0 || x > height || y < 0 || y > width)
return false;
else
return true;
}
And finally my file to use these functions and test output:
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance().row << " " << maze.GetEntrance().col << endl;
cout << "The exit is at: " <<maze.GetExit().row << " " << maze.GetExit().col << endl;
if(maze.IsWall(1,1) == true) //uses the IsWall method to determine wether it is a wall or not
cout << "location (1,1) is a wall\n";
else
cout << "location (1,1) is not a wall\n";
if(maze.IsClear(1,3) == true) //uses the IsClear method to determine wether it is a clear or not
cout << "location (1,3) is clear\n";
else
cout << "location (1,3) is not clear\n";
if(maze.IsInMaze(1,5) == true) //uses the IsInMaze method to determine wether it is a clear or not
cout << "location (1,5) is in the Maze\n";
else
cout << "location (1,5) is not in the Maze\n";
if(maze.IsInMaze(25,35) == true)
cout << "location (25,35) is in the maze\n";
else
cout << "location (25,35) is not in the maze\n";
myIn.close();
return 0;
}
The problem is... I keep getting shotty output. I have included a cout on my "read Maze" function and my "display maze" function to show the difference between the two.
My output:
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
*********************
*********************
*********************
*********************
*********************
*********************
*********************
The entrance is at: 0 18
The exit is at: 6 12
location (1,1) is a wall
location (1,3) is not clear
Height: 7 Width: 20
location (1,5) is in the Maze
Height: 7 Width: 20
location (25,35) is not in the maze
Here is the data file i am reading from:
7 20
0 18
6 12
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
So as you can see, its as if the enum type always thinks that the char is a "Wall" when from the first output of the mazze its clearly not. Any suggestions? Am i missing something that I have probably looked over? Any help is appreciated. Thanks!
In ReadMaze:
Maze[i][k] == Clear;
This is a comparison. I believe you want assignment:
Maze[i][k] = Clear;
The same is true for Maze[i][k] == Wall;.