how can i randomly generate the values of members of struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;
struct rectinfo
{
	int height,width,area,perimeter;
}R1,*R2;


void genData (rectinfo* );
void calAreaPer (rectinfo* );
void display (rectinfo* );
int main()
{
	R2=&R1;
	genData (R2);
	getch();
	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);

}



this is my code and i want to randomly generate the values of height and width......... will someone help plz
1
2
h = rand() % 100 + 1;   // random number 1-100
w = rand() % 100 + 1;


and seed srand
Last edited on
Use the rand() function from <cstdlib>. Then you can make a constructor which will initialize the values when the structure is created.

Hopefully this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib> // for rand() and srand()
#include <ctime>  // for time()
#include <iostream> // for cout and endl
using namespace 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;
}
Last edited on
actually i have to generate the values from using a function......
in my way of inilization thers an error in line
1
2
cout<<"Randomnly generated value of height is "<<R->height(rand()%20);
cout<<"Randomnly generated value of width is "<<R->width(rand()%20);


the error is tht expression must have (pointer-to-) function type
thnx u brother
but isnt there any way tp do it in function gendata(rectinfo*R) ???
Last edited on
i actually have to write a function to randomly generate the values of height and width..............


not by using constructor.......i am new to c++
and this is my home work i google alot but ahh!!! nothing found :(
so i posted here
1
2
3
4
5
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);
}
woww :)
thtx really working :)
and wil u please tell me how these brackets are working ??
i mean how can we assign int to an pointer ?
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.
Topic archived. No new replies allowed.