2016-12-03 173 views
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(); 
} 
+0

你好songyuanyao,我試圖傳遞一個字符串數組「列表」和int「我」,可是你要描述的錯誤消息我帖子。 –

回答

0

你的功能getProblems()void類型的,所以你有什麼用cout顯示?

如果你需要顯示的次數,這是一種說法,

int main() 
{ 
    int count; 
    getProblems(listt,count); //assuming listt, has been declared before 
    cout << "There are " << count << " problems. " << endl;  
    return 0; 
}