last problem

I have almost everything done but these two modules are interacting incorrectly. What happens is it fills the grid with A random letter. its like it only calls the RandomLetters() once and sticks with that character. what can i do to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char RandomLetters()
{
	srand(unsigned (time(NULL)));
	return (rand() % 26 + 'A');
}
void CompletePuzzle(char grid[][c])
{
	char randomchar;
	int i, j; //counters
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(grid[i][j]=='.'){
				randomchar=RandomLetters()
				grid[i][j]=randomchar;
			}
		}
	}
	return;
}
You need to call srand only once in your program.
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
ahah i see so i should not make a module for random letters instead just intigrate it into the module that needs them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CompletePuzzle(char grid[][c])
{
	char randomchar;
	srand(unsigned (time(NULL)));
	int i, j; //counters
	for(i=0;i<r;i++){
		for(j=0;j<c;j++){
			if(grid[i][j]=='.'){
				grid[i][j]=(rand() % 26 + 'A');
			}
		}
	}
	return;
}
No, srand shouldn't be called in any function besides main if you want to be sure it's called only once
i think the define of r & c is missing inside CompletePuzzle() function
Topic archived. No new replies allowed.