Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
int findWord(char *chosenWord)
{
	FILE *dico = NULL;
	int wordsNumber = 0;
	int chosenWordNumber = 0;
	int readCharacter = 0;
	int i = 0;
	
	dico = fopen("dico.txt","r");
	
	if(dico == NULL)
	{
		printf("\nImpossible to load words dictionary !");
		return 0;
	}

	do
	{
		readCharacter = fgetc(dico);
		if( readCharacter == '\n' )
			wordsNumber++;
	} while( readCharacter != EOF );
      
	chosenWordNumber = aleatoryNumber(wordsNumber);

	rewind(dico);
	while( chosenWordNumber > 0 )
	{
		readCharacter = fgetc(dico);
		if(readCharacter == '\n')
			chosenWordNumber--;
	}

	fgets(chosenWord,100,dico);
	chosenWord[strlen(chosenWord) - 1] = '\0';
	fclose(dico);

	return 1;
}

int aleatoryNumber(int maxNumber)
{
	srand(time(NULL));
	return (rand() % maxNumber);
}


What I have tried:

I want a detailed explanation for each line responsible for choosing the random word.
Posted
Updated 19-May-19 21:14pm
v2
Comments
Rick York 20-May-19 0:14am    
You can't always get what you want.

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
You must understand the code yourself, but I can give you an overview. Read some tutorial for learning the language and google for the functions.

The code is reading a file and counting the "\n" so called new lines. After reading a values in the range count is selected by random. Than the file is re-read from the start and the choosen word is copied into a buffer. I would add some output with printf of that.

C++
printf ("The chosen word is: %s\n", chosenWord);
 
Share this answer
 
The line responsible for choosing the random word is
C
chosenWordNumber = aleatoryNumber(wordsNumber);

It calls aleatoryNumber passing the count of found words as argument.

The
C
int aleatoryNumber(int maxNumber)
{
	srand(time(NULL));
	return (rand() % maxNumber);
}
function returns a random number between 0 and (maxNumber-1), because
the rand[^] C library function returns a number between 0 and RAND_MAX and its result is clamped in the 0...maxNumber range by the reminder operator.
Other solutions already explained broadly the full code behaviour.
 
Share this answer
 
Comments
k5054 20-May-19 10:36am    
Just a quick note on aleatoryNumber(): This function calls srand() every time, which resets the random number machinery for each call to aleatoryNumber(). In this case, it appears that aleatoryNumber() is only called once, so that's not a huge issue. However if you had something like
int r1 = aleatoryNumber(10);
int r2 = aleatoryNumber(10);

you might be surprised that r1 == r2 is true more often than not.
Rick York 20-May-19 15:29pm    
srand is using the time value as the seed so the random number sequence will have the same values only if it is called twice within the same second. It should probably called with ( time(NULL) % RAND_MAX ) as the argument actually.
k5054 20-May-19 11:15am    
2nd comment on aleatoryNumber. There are all sorts of problems with rand() in general. See the first 11 minutes of this: rand() Considered Harmful | GoingNative 2013 | Channel 9[^] for a good rundown of the issues with rand(). The remainder of the video covers the <random> header introduced in C++11, so might be worth watching for those wanting better random number generation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900