Function that creates a secret word in C language
kassy
-
kassy -
kassy -
```html
Hello,
here is a function that creates a secret word, I would like to know what the for loop is for in this program, thank you for helping me better understand this function and thank you
174. void creerMotSecret(char *mot) {
175.
176.
177.
178. //Declaration of our local variables
179.
180. FILE *dictionary = NULL;
181.
182. int i = 0, positionWord = 0;
183.
184.
185.
186. //Opening the file in read-only mode
187.
188. dictionary = fopen("dictionary.dat","r");
189.
190.
191.
192. //We take a random number knowing the position of the word in the dictionary that we want
193.
194. //(number between min_position and max_position)
195.
196. srand(time(NULL)); //Initialization
197.
198. positionWord = (rand() % (MAX_POSITION - MIN_POSITION + 1)) + MIN_POSITION;
199.
200.
201.
202.
203. //We choose the word from the dictionary that is at the position "positionWord"
204.
205. for(i = 0; i<positionword i="" />206.
207. fgets(mot,1000,dictionary);
208.
209. }
210.
211.
212.
213. //Closing the file
214.
215. fclose(dictionary);
216.
217.
218.
219. return; //Function "void" so we return nothing
here is a function that creates a secret word, I would like to know what the for loop is for in this program, thank you for helping me better understand this function and thank you
174. void creerMotSecret(char *mot) {
175.
176.
177.
178. //Declaration of our local variables
179.
180. FILE *dictionary = NULL;
181.
182. int i = 0, positionWord = 0;
183.
184.
185.
186. //Opening the file in read-only mode
187.
188. dictionary = fopen("dictionary.dat","r");
189.
190.
191.
192. //We take a random number knowing the position of the word in the dictionary that we want
193.
194. //(number between min_position and max_position)
195.
196. srand(time(NULL)); //Initialization
197.
198. positionWord = (rand() % (MAX_POSITION - MIN_POSITION + 1)) + MIN_POSITION;
199.
200.
201.
202.
203. //We choose the word from the dictionary that is at the position "positionWord"
204.
205. for(i = 0; i<positionword i="" />206.
207. fgets(mot,1000,dictionary);
208.
209. }
210.
211.
212.
213. //Closing the file
214.
215. fclose(dictionary);
216.
217.
218.
219. return; //Function "void" so we return nothing
Configuration: Windows Vista Internet Explorer 7.0```
5 answers
-
-
Thank you Fiddy, I finally understand!!
As for Bigmat or whatever!! People like you shouldn't hang around respectful forums like this one because you pollute the atmosphere. -
positionMot = (rand() % (MAX_POSITION - MIN_POSITION + 1)) + MIN_POSITION;
This statement allows you to choose a pseudo-random number between MIN_POSITION and MAX_POSITION.
for(i = 0; i<positionMot; i++) {
This statement will be executed as long as i<positionMot. Thus, when i equals positionMot, the loop will stop. Since i increases by 1 with each iteration, there will be positionMot iterations.
fgets(mot,1000,dictionnaire);
This statement reads a line from the file. If you execute this statement multiple times, it will read the lines successively. Thus, since fgets is inside the for loop, you will read the line corresponding to the number positionMot.
--
Google is your friend -
Hello,
205. for(i = 0; i<positionMot; i++) { 206. 207. fgets(mot,1000,dictionnaire); 208. 209. }
This loop will read a line from dictionnaire.dat and store it in mot. And we loop positionMot times. Thus we will have the line number positionMot in the file. Hence the secret word.
219. return; //Function "void" so we return nothing
Completely useless instruction in this case ;-))).
--
Google is your friend. -
I am still a beginner, and maybe that’s why I haven’t understood exactly yet. Would you be kind enough to explain a little more?
Thank you.