#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <iostream> // for cout and endl
usingnamespace std;
struct rectinfo
{
int height,width,area,perimeter;
rectinfo() // Constructor, automatically called when you create an object
{
height = rand()%20;
width = rand()%20;
area = height * width;
perimeter = 2*height + 2*width;
}
}; // I don't like making global variables, we'll do this in main
void display(rectinfo* R)
{
cout << "Rondomly generated value of height is " << R->height << endl;
cout << "Randomly generated value of width is " << R->width << endl;
}
int main()
{
srand( time(NULL) );
rectinfo R1; // height and width are automatically generated
rectinfo* R2 = &R1;
display(&R1); // I wanted to demonstrate that you can pass the address without creating R2
display(R2); // This'll show the same thing because it's pointing to the same object
return 0;
}
void genData (rectinfo* R)
{
cout<<"Randomnly generated value of height is "<< (R->height = rand()%20);
cout<<"Randomnly generated value of width is "<< (R->width = rand()%20);
}
I'm not sure I understand what you're referring to by brackets. The parentheses are required in order to keep operator precedence from interfering with what we want to accomplish.
As for the other, we aren't assigning an int to a pointer. We're assigning numbers to the height and width members of the struct that R points to.