C++新手求助:如何将文本文件行分配至数组索引并随机读取?
Fixing Your C++ Random Question/Answer Reader
Hey there! Let's get your code working step by step. I see a few small issues in your snippet, so let's break down how to store those lines properly and pick random ones.
First, let's highlight the key fixes needed:
- You need to store lines in a container first (like an array or vector) before accessing them randomly—your current code tries to read directly to a random number, which won't work.
getline()requires a string variable, not an integer (you tried reading intoquestionNumber, which is an int).- There's a typo in your file check: you opened
qArraybut checked ifaArrayis open.
Here's a complete, working example with explanations:
#include <iostream> #include <fstream> #include <string> #include <vector> // Flexible container for storing lines #include <cstdlib> // For rand() and srand() #include <ctime> // For seeding the random generator using namespace std; // Simplifies code for beginners (you can learn to avoid this later) int main() { // Step 1: Create containers to hold all questions and answers vector<string> questions; vector<string> answers; string line; // Step 2: Load all lines from the questions file ifstream questionFile("Q_Array.txt"); if (questionFile.is_open()) { while (getline(questionFile, line)) { questions.push_back(line); // Add each line to the questions list } questionFile.close(); } else { cout << "Oops, couldn't open the questions file!" << endl; return 1; // Exit with error if file fails to open } // Step 3: Load all lines from the answers file ifstream answerFile("A_Array.txt"); if (answerFile.is_open()) { while (getline(answerFile, line)) { answers.push_back(line); // Add each line to the answers list } answerFile.close(); } else { cout << "Oops, couldn't open the answers file!" << endl; return 1; } // Step 4: Seed the random generator (do this ONCE at the start) srand(time(0)); // Step 5: Generate a random index (0 to 99, since we have 100 lines) int randomIndex = rand() % 100; // Step 6: Show the random question and its matching answer cout << "Random Question: " << questions[randomIndex] << endl; cout << "Answer: " << answers[randomIndex] << endl; return 0; }
Quick Tips for You:
- Vectors vs Fixed Arrays: I used
vector<string>because it automatically handles size (no need to pre-declare 100 elements). If you prefer a fixed array instead, replace the vectors with:
Then use an index variable to assign lines in the loop:string questions[100]; string answers[100];int i = 0; while (getline(questionFile, line) && i < 100) { questions[i] = line; i++; } - File Locations: Make sure your
.txtfiles are in the same folder as your program's executable, or use the full file path (like"C:/MyProjects/Q_Array.txt"). - Loop for Multiple Questions: If you want to keep picking random questions, wrap the random index and display code in a
while(true)loop, and add a way to exit (like checking for a key press).
内容的提问来源于stack exchange,提问作者B.Crim
相关产品推荐
相关产品推荐

