can someone help me? i need to make a program that prints out 10 random questions from the 15 questions in a .txt file, and then print out the choice IN RANDOM. please do help me. i really need to finish this.
can someone help me? i need to make a program that prints out 10 random questions from the 15 questions in a .txt file, and then print out the choice IN RANDOM. please do help me. i really need to finish this.
What idea do you have to solve this? Can you write a program that prints out the last 10 questions from the 15 questions in that file?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> please do help me. i really need to finish this.
Well that suggests that you've at least made a start.
Perhaps you should post a specific question demonstrating the point you're stuck on.
There's no point us explaining how to read a file (for example), when all you care about is the random order aspect of the problem.
> can someone help me?
How To Ask Questions The Smart Way
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
sorry, i'm just new here. so here's what i got, just trying to print out 10 random numbers out of 15 from a txt file.
and, it doesn't work. what am i doing wrong?Code:#include<iostream> #include<conio.h> #include<stdlib.h> #include<time.h> int main () { FILE *fp; fp=fopen("D:\\2.txt", "r"); int stk[10]; srand ((unsigned)time(NULL)); for (int c=0; c<9; c++) { int x = rand ()%10; fgets(stk[c],50,fp+x); fp=fp-x; } fclose(fp); system("pause>0"); }
The fp pointer does not point to the number of lines or anything that would help you make a choice among which questions to use. It simply points to the location (memory address) of the stuff needed to actually use your file. fp+x actually changes the address you read from.and, it doesn't work. what am i doing wrong?
Of course, people would also take issue with the fact that you're using the C library despite not including it.
You can use seekg() to do what you were attempting, though. Note that this way uses C++ streams.
can you please teach me how to make the fp pointer point to the number of lines?
How about starting with the basics.
In C++ (note, this means using iostream and not stdio), write a program to just print every line of the file.
No randomness, no "10 from 15", no choices - just print the file.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.