【发布时间】:2017-04-03 02:58:52
【问题描述】:
我正在尝试创建一个程序,用户可以在其中输入一系列球员姓名和分数并将它们读回。但是,我无法使用 getline 存储他们的输入。在 InputData 函数中的 getline 上,Visual Studio 指出,“错误:没有重载函数的实例“getline”与参数列表参数类型匹配:(std::istream, char)”,并且在 == 上,它说,“错误: 操作数类型不兼容(“char”和“const char *”)”。这是我的代码:
#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;
}
【问题讨论】: