|
-
Nov 16th, 2001, 05:15 PM
#1
Thread Starter
Hyperactive Member
Picking from a text file at random?
Im tryign to make my program (a trivia one) read from a text file, well, it reads the questions from the text file.
But im trying to make it pick the questions at random. I cant find any tutorials for this, so wondered if anyone here could help..
thanx
Power to 2000 Electronic Donkeys!
www.edonkey2000.com
I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".
-
Nov 16th, 2001, 06:08 PM
#2
Code:
#include <stdio.h>
void main(void){
char arr[100][100]; // one hundred questions
char tmp[100];
int i,j;
int q=(-1);
FILE*in;
in = fopen("myquestions.txt","r");
if (ferror(in) || in ==NULL){
perror("Error opening input file");
exit(1);
}
memset (tmp,'\0',sizeof(tmp) );
while (!feof(in) ){
if(fgets(tmp,99,in)!=NULL) {
q++;
strcpy(arr(q),tmp);
}
} // you now have a char array of length q
fclose(in);
for (i=0;i<10;i++) { // ask 10 questions
j = (int) ( ( (float)rand()/ (float)RAND_MAX ) *(float)(q +1) ) ;
// j is a random value between zero and q
printf("question: %s ?\n", arr[j]);
}
}
-
Nov 17th, 2001, 05:02 AM
#3
Thread Starter
Hyperactive Member
Power to 2000 Electronic Donkeys!
www.edonkey2000.com
I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".
-
Nov 17th, 2001, 02:58 PM
#4
j = (int) ( ( (float)rand()/ (float)RAND_MAX ) *(float)(q +1) ) ;
what is the advantage of this (it's awfully slow...)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 19th, 2001, 10:07 AM
#5
There is no great advantage to this, other than eliminating the use of more variables declared as float.
You're right - casting is very slow - but this app probably does one calculation like this every 30 seconds. It isn't in a loop executing at full cpu speed. If it were it would be a terrible choice.
-
Nov 19th, 2001, 11:09 AM
#6
I mean what is the advantage of this vs
int i = rand() % q;
?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|