0
晚上好,我试图在主函数中调用我的void函数“getProblems”,但是在输出没有参数的“getProblems”时会得到一个额外的值。同样,当传递参数时,如“getProblems(list,i)”,我得到错误“没有操作符”< <'匹配这些操作数“。目标是输出我的文本文件包含的问题数量,而不使用返回值或使用指针的函数。如何使用数组和参考参数调用void函数
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
int const MAX_PROBLEMS = 50;
void getProblems(string problem[], int& count);
int main()
{
string list[MAX_PROBLEMS] = {};
int i = 0;
cout << "There are " << getProblems << " problems. " << endl;
// I have also tried calling the void function with parameters
// cout << "There are " << getProblems(list, i) << "problems. " << endl;
return 0;
}
void getProblems(string problem[], int& count)
{
ifstream mathProblems;
mathProblems.open("P4Problems.txt");
if (!mathProblems)
{
cout <<"No file was found."<< endl;
}
count = 0;
string data;
getline(mathProblems, data);
while (!mathProblems.eof())
{
problem[count] = data;
count ++;
mathProblems >> data;
}
mathProblems.close();
}
你好songyuanyao,我试图传递一个字符串数组“列表”和int“我”,可是你要描述的错误消息我帖子。 –