One of the assignments for my class is to read in a text file with questions, answers and the correct number. I am not sure while my file is not reading correctly, but for some reason I am not getting the information to pass to my class file from the initializing function.
我班级的一项任务是在一个文本文件中读入问题,答案和正确的数字。我不确定我的文件读取不正确,但由于某种原因,我没有从初始化函数传递给我的类文件的信息。
void initQuestions(Question q[])
{
ifstream infile; // Input file to read questions into array
infile.open("questions.txt");
int pos; // loop counter
string quest; // variable holding question text
string posAns[numAns]; // variable array holding the possible answers
int corrAns; // variable holding the correct answer
// Error message if file unable to open
if (!infile.good())
{
cout << "Error: Unable to open file!" << endl;
}
while (infile)
{
// Load variables and array from the file using while loop
getline(infile, quest);
for (int i = 0; i < numAns; i++)
getline(infile, posAns[i]);
infile >> corrAns;
for (pos = 0; pos < questNum ; pos++)
{
q[pos].setQuestionText(quest);
q[pos].setPossAnswer1(&posAns[0]);
q[pos].setPossAnswer2(&posAns[1]);
q[pos].setPossAnswer3(&posAns[2]);
q[pos].setPossAnswer4(&posAns[3]);
q[pos].setPossAnswer5(&posAns[4]);
q[pos].setCorrect(corrAns);
getline(infile, quest);
for (int i = 0; i < numAns; i++)
getline(infile, posAns[i]);
infile >> corrAns;
}
}
}
This is my function. The call I am using in main is:
这是我的功能。我主要使用的电话是:
initQuestions(&quiz[questNum]);
Any help would be much appreciated. Thanks.
任何帮助将非常感激。谢谢。
Debugger Output:
0x00007ffff7b8d44b in std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string(std::strin
g const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) continue
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)
1 个解决方案
#1
0
There are some issues with how you call the function from your main.
如何从main调用函数有一些问题。
First off when you pass an array you have to just pass the array and its size, secondly an array is passed by reference in c++ so you don't want the &
so:
首先,当你传递一个数组时,你必须传递数组及其大小,其次,数组通过c ++中的引用传递,所以你不需要&&:
Main:
initQuestions(quiz , questNum);
You method declaration would also have to change:
方法声明也必须更改:
void initQuestions(Question q[], int size)
For help with passing an array see: https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
有关传递数组的帮助,请参阅:https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Also when reading in a file your should read line by line and check if the line is still good in the while loop. You are checking if the file itself is good.
另外,在读取文件时,您应该逐行读取并检查while循环中的行是否仍然良好。您正在检查文件本身是否良好。
There are a lot of questionable things you are doing to read in the file so I highly recommend checking out this resource for examples: http://www.cplusplus.com/doc/tutorial/files/
您在文件中阅读的内容有很多值得怀疑的内容,因此我强烈建议您查看此资源以获取示例:http://www.cplusplus.com/doc/tutorial/files/
#1
0
There are some issues with how you call the function from your main.
如何从main调用函数有一些问题。
First off when you pass an array you have to just pass the array and its size, secondly an array is passed by reference in c++ so you don't want the &
so:
首先,当你传递一个数组时,你必须传递数组及其大小,其次,数组通过c ++中的引用传递,所以你不需要&&:
Main:
initQuestions(quiz , questNum);
You method declaration would also have to change:
方法声明也必须更改:
void initQuestions(Question q[], int size)
For help with passing an array see: https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
有关传递数组的帮助,请参阅:https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Also when reading in a file your should read line by line and check if the line is still good in the while loop. You are checking if the file itself is good.
另外,在读取文件时,您应该逐行读取并检查while循环中的行是否仍然良好。您正在检查文件本身是否良好。
There are a lot of questionable things you are doing to read in the file so I highly recommend checking out this resource for examples: http://www.cplusplus.com/doc/tutorial/files/
您在文件中阅读的内容有很多值得怀疑的内容,因此我强烈建议您查看此资源以获取示例:http://www.cplusplus.com/doc/tutorial/files/