2017-04-03 42 views
1

我試圖創建一個程序中,用戶可以輸入一系列球員的名字和分數和閱讀他們回來。但是,我無法使用getline存儲他們的輸入。在InputData函數的getline中,visual studio聲明:「錯誤:沒有重載函數的實例」getline「與參數列表參數類型匹配的是:(std :: istream,char)」,on ==,它表示「Error :操作數類型不兼容(「char」和「const char *」)「。這裏是我的代碼:C++版本使用函數getline

#include <iostream> 
#include <string> 

using namespace std; 

int InputData(string [], int [], int); 
void DisplayPlayerData(string [], int [], int); 

void main() 
{ 
    string playerNames[100]; 
    int scores[100]; 

    int sizeOfArray = sizeof(scores); 
    int sizeOfEachElement = sizeof(scores[0]); 
    int numberOfElements = sizeOfArray/sizeOfEachElement; 

    cout << numberOfElements; 

    int numberEntered = InputData(playerNames, scores, numberOfElements); 

    DisplayPlayerData(playerNames, scores, numberOfElements); 

    cin.ignore(); 
    cin.get(); 
} 

int InputData(string playerNames, int scores[], int size) 
{ 
    int index; 


    for (index = 0; index < size; index++) 
    { 
     cout << "Enter Player Name (Q to quit): "; 
     getline(cin, playerNames[index]); 
     if (playerNames[index] == "Q") 
     { 
      break; 
     } 
     cout << "Enter score: "; 
     cin >> scores[index]; 
    } 

    return index; 
} 

回答

2

int InputData(string playerNames, int scores[], int size)

應該

int InputData(string playerNames[], int scores[], int size)


在代碼中,你通過playerNames作爲一個字符串,而不是字符串數組。

getline(cin, playerNames[index]);playerNames[index]是一個字符,因爲playerNames是一個字符串。

+0

是的,這是我做錯了什麼。非常感謝你。 – jackofblaze

+0

我的樂趣:) @jackofblaze – Jiahao

+0

我有一個問題一個進一步的問題與我的函數getline,我應該問這裏或啓動一個新的問題呢?我只是在getline在第一次運行後跳過任何用戶輸入時遇到問題,並且這樣做直到它通過所有數組元素。 – jackofblaze

0

,因爲它是因爲我已經用C++很長一段時間,但你嘗試過cin.getline,而不只是函數getline我可能是錯的?

1

Error: no instance of overloaded function "getline" matches the argument list argument types are: (std::istream, char)"

這意味着你是一個字符值傳遞給getline()而不是整個的。你這樣做的:

getline(cin, playerNames[index]) 

您傳遞playernames作爲一個字符串變量,而不是一個sting[]陣列。所以,當你做playernames[index],你試圖通過一個char值的功能。

main()功能從代碼來看,你想爲一個字符串陣列傳遞給函數,而不是僅僅一個單串。

因此,改變InputData()

int InputData(string playerNames, int scores[], int size) 

的參數:

int InputData(string playerNames[], int scores[], int size) 

** @Pinky打我首先發布的答案:),但我想提供一些更多的細節,所以我發佈了我的答案。

另一件事,除此之外,我想只是指出你的主要問題,您應該將main()函數的返回數據類型從void main()更改爲int main()