I'm very new at this but I'm getting some weird error:

unexpected end of file while looking for precompiled header directive

I don't have much yet but here it is:

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
using namespace std;

class Rectangle
{
public:
    Rectangle(); //constructor
    ~Rectangle(); //destructor
    Rectangle();  //copy constructor
    friend void intersect ();
    friend void union_function();
    friend void diff();
    friend void concat();
    friend void another();
    void print_rectangle();

private:
        //need a pointer to dynamically allocated array of points 
    int size;
};
#endif

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle::Rectangle()
{
    //Constructor
    int x1= y1 = x2 = y2 = 0; //initializes all variables to 0
}

Rectangle::~Rectangle()
{
    //Destructor
}

Rectangle::Rectangle()
{
    //Need to have a copy constructor
}

void intersect()
{
    //Friend Function to construct intersection of two rectangles
    //The intersection of two rectangles is the set of points       
            //that are in both rectangles. 
    //intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
}

void union_function()
{
    //Friend Function to determine the union of the two rectangles.  The
    //union of two rectangles is the set of points in both rectangles.
    //union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
}

void diff()
{
    //Friend Function to determine the difference of two rectangles.  A 
    // difference of two retangles is the set of points that are in the 
    //first rectangle but not in the second.
    //difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
}

void concat()
{
    //Member Function to concatenate another rectangle on the right.  
    //So R1.concatright(R2) will form the union of all the points in R1 
    //and a modification of all the points in R2The x-coordinate of all of 
    //the points in R2 are increased by one plus the maximum    //x-coordinate of all of the points in R1.
             //R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
             //in R2 and x'= x+1+maxX(R2)}
            //where maxX(R2) is the maximum of all the x values of the points in 
    //R2.  It may not actually be a rectangle as defined, but we still call 
             //the resule a "rectangle".
}

void another()
{
    //Member Function to concatenate another rectagle above.  So 
    //R1.concatabove(R2) will form the union of all the points in R1 and 
    //modification of all the points in R2.  The y-coordinate of all the 
    //points in R2 are increased by one plus the maximum
    // y-coordinate of all of the points in R1.  It may not actually be a 
    //rectangle as defined, but we still call the resule a "rectangle".  
            //R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and 
            //y= y'+1+maxY(R2)}
           //where maxY(R2) is the maximum of all of the y values of the points i
           //n R2.
}

Rectangle::print_rectangle()
{
    //Member function to print out the results in pictorial form 
}

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    int x1 = y1 = x2 = y2 = 0;

    cout << "Please enter two sets of (x,y) coordinates: ";
    cin << x1 << y1 << x2 << y2;

    return 0;
}

Umm I don't understand what you are saying Dave, I'm just curious why I'm getting that error.

Thanks

Never mind I got it thanks

Need more help I got the program to compile with 0 errors!!!

Now I asked the user for two sets of (x,y) coordinates and I would like to print the rectangle. Can someone help me implement the print_rectangle function. How do I pass the (x,y) coordinates to that function.

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;

    Rectangle R1, R2;

    cout << "Please enter two sets of (x,y) coordinates: ";
    cin >> x1 >> y1 >> x2 >> y2;

    R1.print_rectangle();

    return 0;
}

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle::Rectangle()
{
//Constructor
int x1= 0; //initializes all variables to 0
int y1= 0;
int x2= 0;
int y2= 0;
}

//Still can't get the destructor to compile
Rectangle::~Rectangle()
{
//Destructor
}

Rectangle::Rectangle()
{
//Need to have a copy constructor
}

void intersect()
{
//Friend Function to construct intersection of two rectangles
//The intersection of two rectangles is the set of points 
//that are in both rectangles. 
//intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
}

void union_function()
{
//Friend Function to determine the union of the two rectangles. The
//union of two rectangles is the set of points in both rectangles.
//union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
}

void diff()
{
//Friend Function to determine the difference of two rectangles. A 
// difference of two retangles is the set of points that are in the 
//first rectangle but not in the second.
//difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
}

void concat()
{
//Member Function to concatenate another rectangle on the right. 
//So R1.concatright(R2) will form the union of all the points in R1 
//and a modification of all the points in R2The x-coordinate of all of 
//the points in R2 are increased by one plus the maximum //x-coordinate of all of the points in R1.
//R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
//in R2 and x'= x+1+maxX(R2)}
//where maxX(R2) is the maximum of all the x values of the points in 
//R2. It may not actually be a rectangle as defined, but we still call 
//the resule a "rectangle".
}

void another()
{
//Member Function to concatenate another rectagle above. So 
//R1.concatabove(R2) will form the union of all the points in R1 and 
//modification of all the points in R2. The y-coordinate of all the 
//points in R2 are increased by one plus the maximum
// y-coordinate of all of the points in R1. It may not actually be a 
//rectangle as defined, but we still call the resule a "rectangle". 
//R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and 
//y= y'+1+maxY(R2)}
//where maxY(R2) is the maximum of all of the y values of the points i
//n R2.
}

void Rectangle:print_rectangle()
{
//Member function to print out the results in pictorial form 
}

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
using namespace std;

class Rectangle
{
public:
    Rectangle(); //constructor
    ~Rectangle(); //destructor
    //Rectangle();  //copy constructor
    friend void intersect ();
    friend void union_function();
    friend void diff();
    friend void concat();
    friend void another();
    void print_rectangle();

private:
        //pointer to dynamically allocated array of points 
    int size;
};
#endif

Went ahead and merged the two threads together. Let's try and keep the same discussion in one thread. :)

I got the print_rectangle function implemented and working. Now I'm supposed to have a pointer to a dynamically allocated array of points as a private data member and I'm a little stuck with this part.

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;

Rectangle R1, R2;

    cout << "Please enter two sets of (x,y) coordinates: ";

    R1.print_rectangle(x1, y1);
    R2.print_rectangle(x2, y2);
return 0;
}

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle::Rectangle()
{
//Constructor
int x1= 0; //initializes all variables to 0
int y1= 0;
int x2= 0;
int y2= 0;
}

//Still can't get the destructor to compile
Rectangle::~Rectangle()
{
//Destructor
}

Rectangle::Rectangle()
{
//Need to have a copy constructor
}

void intersect()
{
//Friend Function to construct intersection of two rectangles
//The intersection of two rectangles is the set of points 
//that are in both rectangles. 
//intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
}

void union_function()
{
//Friend Function to determine the union of the two rectangles. The
//union of two rectangles is the set of points in both rectangles.
//union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
}

void diff()
{
//Friend Function to determine the difference of two rectangles. A 
// difference of two retangles is the set of points that are in the 
//first rectangle but not in the second.
//difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
}

void concat()
{
//Member Function to concatenate another rectangle on the right. 
//So R1.concatright(R2) will form the union of all the points in R1 
//and a modification of all the points in R2The x-coordinate of all of 
//the points in R2 are increased by one plus the maximum //x-coordinate of all of the points in R1.
//R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
//in R2 and x'= x+1+maxX(R2)}
//where maxX(R2) is the maximum of all the x values of the points in 
//R2. It may not actually be a rectangle as defined, but we still call 
//the resule a "rectangle".
}

void another()
{
//Member Function to concatenate another rectagle above. So 
//R1.concatabove(R2) will form the union of all the points in R1 and 
//modification of all the points in R2. The y-coordinate of all the 
//points in R2 are increased by one plus the maximum
// y-coordinate of all of the points in R1. It may not actually be a 
//rectangle as defined, but we still call the resule a "rectangle". 
//R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and 
//y= y'+1+maxY(R2)}
//where maxY(R2) is the maximum of all of the y values of the points i
//n R2.
}

void Rectangle::print_rectangle(int x, int y)
{
             while (cin >> x >> y) 
    {
        for (int row=0; row<y; row++) 
        {
            for (int col=0; col<x; col++) 
            {
                cout << "*";
            }
            cout << endl;  // end the line of stars.
        }
        cout << endl;
    }
}

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
using namespace std;

class Rectangle
{
public:
Rectangle(); //constructor
~Rectangle(); //destructor
//Rectangle(); //copy constructor
friend void intersect ();
friend void union_function();
friend void diff();
friend void concat();
friend void another();
void print_rectangle(int, int);

private:
int size;
};
#endif

Greetings coolmel55,

Creating a dynamically allocated array is not hard what so ever. You just need to know your allocation size, and how much data you intend to store.

As a private member, you would probably create a variable that looks similar to this:

int *points;

The unary operator * is the indirection of dereferecing operator. When applied to a pointer, it accesses the object the pointer points to. It would be impossible to send data to our new integer-type pointer points because we have no memory block to write to. A pointer is simply a variable that contains the address of a variable. A pointer is a group of cells (often two or four) that can hold an address.

To properly allocate memory to our pointer, we can use the standard function malloc() and free() included in stdlib. These functions are not hard to use at all. I linked the two functions to a good reference page in case you are looking for greater detail.

Here is an example of how we would allocate memory to our integer-pointer:

#include <stdlib.h>

int main() {
	int *points;

	// Allocate memory to hold 2 ints
	points = (int *)malloc(2 * sizeof *points);

	// Write to our two ints
	points[0] = 1;
	points[1] = 5;

	// Free memory
	free(points);

	return 0;
}

Example 1.1: Allocating memory

You can use the same idea to allocate memory for your points. Just set points[0] to x, and points[1] to y, or else however you'd like to do it.


Hope this helps,
- Stack Overflow

Had a chance to implement the dynamically allocated array but I did it in main and I'm supposed to add it in the private data memeber section of the Rectangle class.

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    int *points;

    // Allocate memory to hold 4 ints
    points = (int *)malloc(4 * sizeof *points);


    Rectangle R1, R2;

    cout << "Please enter two sets of (x,y) coordinates: ";

    R1.print_rectangle(points[0], points[1], points[2], points[3]);

    // Free memory
    free(points);

    return 0;


}

Ah,

Well if I am correct, you should be able to do something like:

struct Test {
public:
	// Need a function inside structure to set *points
	void allocPoints(int x, int y);
	void cleanUp(void);
private:
	// Nothing can touch this outside of the structure
	int *points;
};

int main() {
	// Local variables
	int x1 = 0, x2 = 0;
	int y1 = 0, y2 = 0;
	Test R1, R2;

	// Send coordinates to structure
	R1.allocPoints(x1, y1);
	R2.allocPoints(x2, y2);

	// Free our memory
	R1.cleanUp();
	R2.cleanup();

	return 0;
}

// We are inside our structure
void Test::allocPoints(int x, int y) {
	// We have direct access to our private member, allocate memory
	points = (int *)malloc(2 * sizeof(*points));

	// Set them here
	points[0] = x;
	points[1] = y;
}

void Test::cleanUp(void) {
	// Make sure the variable exists
	if (points)
		// If so, free memory
		free(points);
}

Hope this makes sense, and I hope this helps. Please feel free to ask further questions.

- Stack Overflow

Thanks Stack,

That makes perfect sense except I re-read the assignment and the part that I'm stuck with is that the rectangle we are going to print has two sets of points (x1, y1, (x2, y2) these are the lower left hand corner the the upper right hand corner points of the rectangle. So it would have to be a two-dimensional array dynamically created.

Allocating a two-dimensional array isn't difficult at all.

To get the geist of how they work check out this thread. Now, for dynamic allocation we must take a different approach. Lets take a look on how this can be done:

#include <stdlib.h>

// Declare function
int **dynArray(int, int);

int main() {
	int rows;	// Amount of rows
	int **p;	// Two-dimensional pointer

	// Dynamically create an array of [2][2]
	p = dynArray(2, 2);

	// Set stuff to our variables
	p[0][0] = 1;
	p[1][0] = 3;
	// ...So on and so forth

	// Free memory
	for (rows = 0; rows < 2; rows++)
		free(p[rows]);
	free(p);

	return 0;
}

// Our own function to dynamically create an array
int **dynArray(int row, int col) {
	int r;
	int **temp;

	// Allocate rows
	temp = (int **)malloc(row * sizeof(int *));
	// Go through all rows
	for (r = 0; r < row; r++)
		// Allocate columns
		temp[r] = (int *)malloc(col * sizeof(int));
	return temp;
}

Hope this makes sense. We pretty much wrote our own function to alloate the memory, and we will free it once we are done with it. If you have any further questions, please feel free to ask.


- Stack Overflow

but I can't write my own function according to the instructions given we can only use the functions he is giving us?

I'm guessing so, as it states: You may only use the C++ features that we will be reviewing in the first two segments of the course.

- Stack Overflow

Are you trying to do something like this?

#include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;
  
  int main()
  {
   int size = 4;
        int *point = new int[size];
        cout << "x1? ";
        cin  >> point[0];
        cout << "y1? ";
        cin  >> point[1];
        cout << "x2? ";
        cin  >> point[2];
        cout << "y2? ";
        cin  >> point[3];
   for ( int i = min(point[1], point[3]); i <= max(point[1], point[3]); ++i )
    {
 	  for ( int j = min(point[0], point[2]); j <= max(point[0], point[2]); ++j )
 	  {
		 cout << 'x';
	  }
	  cout << endl;
  }
  delete[] point;
  return 0;
  }
  
  /* my output
x1? 2
 y1? 3
 x2? 12
 y2? 5
 xxxxxxxxxxx
 xxxxxxxxxxx
 xxxxxxxxxxx
 */

Yes that is what I need to do but I'm having difficulty with the class and the private data members as I'm required to have the pointer to the dynamically allocated array as a private data member and I need to have the size as a private data member and the print function is a function of the class.

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
using namespace std;

class Rectangle
{
public:
    Rectangle();  //constructor
    Rectangle(int, int, int, int); //copy constructor
    ~Rectangle(); //destructor
    friend void intersect ();
    friend void union_function();
    friend void diff();
    friend void concat();
    friend void another();
    void print_rectangle();

private:
    int size;
    int *point = new int[size];
};
#endif

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle::Rectangle()
{
//Constructor
int x1= 0; //initializes all variables to 0
int y1= 0;
int x2= 0;
int y2= 0;
}

//Still can't get the destructor to compile
Rectangle::~Rectangle()
{
//Destructor
}

Rectangle::Rectangle()
{
//Need to have a copy constructor
}

void intersect()
{
//Friend Function to construct intersection of two rectangles
//The intersection of two rectangles is the set of points 
//that are in both rectangles. 
//intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
}

void union_function()
{
//Friend Function to determine the union of the two rectangles. The
//union of two rectangles is the set of points in both rectangles.
//union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
}

void diff()
{
//Friend Function to determine the difference of two rectangles. A 
// difference of two retangles is the set of points that are in the 
//first rectangle but not in the second.
//difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
}

void concat()
{
//Member Function to concatenate another rectangle on the right. 
//So R1.concatright(R2) will form the union of all the points in R1 
//and a modification of all the points in R2The x-coordinate of all of 
//the points in R2 are increased by one plus the maximum //x-coordinate of all of the points in R1.
//R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
//in R2 and x'= x+1+maxX(R2)}
//where maxX(R2) is the maximum of all the x values of the points in 
//R2. It may not actually be a rectangle as defined, but we still call 
//the resule a "rectangle".
}

void another()
{
//Member Function to concatenate another rectagle above. So 
//R1.concatabove(R2) will form the union of all the points in R1 and 
//modification of all the points in R2. The y-coordinate of all the 
//points in R2 are increased by one plus the maximum
// y-coordinate of all of the points in R1. It may not actually be a 
//rectangle as defined, but we still call the resule a "rectangle". 
//R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and 
//y= y'+1+maxY(R2)}
//where maxY(R2) is the maximum of all of the y values of the points i
//n R2.
}

void Rectangle:print_rectangle(int x, int y)
{
    for ( int i = min(point[1], point[3]); i <= max(point[1], point[3]); ++i )
    {
      for ( int j = min(point[0], point[2]); j <= max(point[0], point[2]); ++j )
      {
         cout << 'x';
      }
      cout << endl;
    }
}

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{


        cout << "Please input two sets of coordinates one point at a time";
        cin  >> point[0] >> point[1] >> point[2] >> point[3];



    return 0;

}

Getting a million errors because I don't know how to use the array and pointer

Move the input and such to the constructor and print member functions.

#include <iostream>
 using std::cin;
 using std::cout;
 using std::endl;
 
 class Rectangle
 {
    int size;
    int *point;
 public:
    Rectangle(int s) : size(s)
    {
 	  point = new int[size];
 	  cout << "x1? ";
 	  cin  >> point[0];
 	  cout << "y1? ";
 	  cin  >> point[1];
 	  cout << "x2? ";
 	  cin  >> point[2];
 	  cout << "y2? ";
 	  cin  >> point[3];
    }
    Rectangle(int, int, int, int); //copy constructor
    ~Rectangle() //destructor
    {
 	  delete[] point;
    }
    friend void intersect ();
    friend void union_function();
    friend void diff();
    friend void concat();
    friend void another();
    void print_rectangle()
    {
 	  int a = min(point[1], point[3]);
 	  int b = max(point[1], point[3]);
 	  int c = min(point[0], point[2]);
 	  int d = max(point[0], point[2]);
 	  for ( int i = a; i <= b; ++i )
 	  {
 		 for ( int j = c; j <= d; ++j )
 		 {
 			cout << 'x';
 		 }
 		 cout << endl;
 	  }
    }
 };
 
 int main()
 {
    Rectangle r(4);
    r.print_rectangle();
    return 0;
 }
 
 /* my output
 x1? 2
 y1? 3
 x2? 12
 y2? 5
 xxxxxxxxxxx
 xxxxxxxxxxx
 xxxxxxxxxxx
 */

Getting these errors:

error C2664: '__thiscall Rectangle::Rectangle(const class Rectangle &)' : cannot convert parameter 1 from 'const int' to 'const class Rectangle &'
Reason: cannot convert from 'const int' to 'const class Rectangle'
No constructor could take the source type, or constructor overload resolution was ambiguous

error C2065: 'min' : undeclared identifier

error C2065: 'max' : undeclared identifier

I hate it when my compiler lets these things slide. I think they should properly be included if you add this.

#include <algorithm>
 using std::min;
 using std::max;

Still getting the errors.

wouldn't using namespace std; handle the using std::min;
using std::max;

Shoot. MSVC. I try to do standard stuff, but some compilers are stubborn -- and others let me get away with no-nos.

Try this instead.

#define min(a,b) ((a) < (b) ? (a) : (b))
 #define max(a,b) ((a) > (b) ? (a) : (b))

Ok soo it appears that this program maybe a little easier than I was making it, but nothing new. I sometimes make things a lot harder than they need to be. I don't have to take user input for this program. I also need another class called Points. I need to encorporate the use of a dynamically allocated multidimensional array.

Any ideas based on what I already have?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.