我試圖創建一個程序中,用戶可以輸入一系列球員的名字和分數和閱讀他們回來。但是,我無法使用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;
}
是的,這是我做錯了什麼。非常感謝你。 – jackofblaze
我的樂趣:) @jackofblaze – Jiahao
我有一個問題一個進一步的問題與我的函數getline,我應該問這裏或啓動一個新的問題呢?我只是在getline在第一次運行後跳過任何用戶輸入時遇到問題,並且這樣做直到它通過所有數組元素。 – jackofblaze