0
How do we randomly read non repeating 20 lines from file?
I have to read 20 lines from a file containg 100 lines .Those lines should not be read twice.
9 Antworten
+ 8
Well if you load the entire file to memory, create an array [20] of unique numbers (init to zero, fill each element after checking all previous ones <--- through recursion), then you create a vector/array of 20 strings read from memory and finally trash the cache (delete the memory holding the file).
What part are you struggling with? That info would help get a better answer.
+ 3
Don't worry I provide you with a simple example and some tricks to illustrate how it going to work correctly.
+ 2
Very interesting question!
We love to see the code along with your question because we are programmers!
We love to do it if we see your best shot because we need motivation!
We love to appreciate your effort because we know how hard it can be to formulate a solution!
+ 2
"I don't know how can i share the whole project containing various files with you [...]"
First, give us a sample of that text file. It'd be awesome if you copy/paste a portion of the text file that has to be read by the program, here.
Second, create a C++ code here and copy/paste all files there (seperate them by a simple comment like /*** file1.h, file1.cpp, etc.***/ ), save it and attach it here for inspection.
Third, I'll be more than happy to see what I can do for you.
Note: If all of them was cumbersome for you to do, then tell me to figure out something else.
+ 2
Text file:
1. How many programmers do we have in our city?
2. Were you satisfied with your salary?
3. How much experience do you have?
4. What was your last position?
5. What do you have to offer?
6. How many languages do you know?
7. Which language is your favorite?
8. question 8
9. question 9
10. question 10
~~~~~~~~
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
srand(static_cast<time_t>(time(nullptr)));
const size_t number_of_questions = 10;
size_t i = 0;
bool lookup[number_of_questions] = {false}; // keeping track of previously generated randoms
vector<string> vs(number_of_questions + 1); // hold the file's content
ifstream in("questions.txt");
while (getline(in, vs[i++])); // get the file's whole content
size_t number_of_randoms = 3;
while (number_of_randoms) { // pick three random question without repetition
int current_random = rand() % 10;
if (!lookup[current_random]) {
cout << vs[current_random] << endl;
++lookup[current_random];
--number_of_randoms;
}
}
in.close();
}
Sample Output:
4. What was your last position?
1. How many programmers do we have in our city?
10. question 10
+ 1
C++ Soldier (Babak) I'm actually working on a project of exam management system where a examiner can store as many questions as he want and at the time of exam he randomly select the questions and present it in front of students. I don't know how can i share the whole project containing various files with you but i hope you got an idea of what i am taking about
+ 1
C++ Soldier (Babak) i would be very happy to share it with you if you can give me your email and I'll provide you my code in Ms word ..If you're not comfortable with it, you can just help me by creating a file and putting few strings in it and printing out some of them without repeation.Thank you ☺
+ 1
C++ Soldier (Babak) appreciated :)
+ 1
thanks a lot for your time :) C++ Soldier (Babak)