C++ C++;使用Getline发布
我正在尝试创建一个程序,用户可以在其中输入一系列球员的名字和分数,并将其读回。但是,我在使用getline存储它们的输入时遇到了问题。在InputData函数的getline上,VisualStudio声明,“错误:重载函数“getline”的任何实例都与参数列表匹配参数类型为:(std::istream,char)”,而On==,表示“错误:操作数类型不兼容(“char”和“const char*”)。这是我的密码:C++ C++;使用Getline发布,c++,getline,C++,Getline,我正在尝试创建一个程序,用户可以在其中输入一系列球员的名字和分数,并将其读回。但是,我在使用getline存储它们的输入时遇到了问题。在InputData函数的getline上,VisualStudio声明,“错误:重载函数“getline”的任何实例都与参数列表匹配参数类型为:(std::istream,char)”,而On==,表示“错误:操作数类型不兼容(“char”和“const char*”)。这是我的密码: #include <iostream> #include <
#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;
}
#包括
#包括
使用名称空间std;
int-InputData(字符串[],int[],int);
void displayerData(字符串[],整数[],整数);
void main()
{
弦乐演奏家姓名[100];
整数分数[100];
int sizeOfArray=sizeof(分数);
int-sizeOfEachElement=sizeof(分数[0]);
int numberOfElements=sizeOfArray/sizeofeachement;
coutint-InputData(字符串播放名称,int-scores[],int-size)
应该是
int-InputData(字符串playerNames[],int-scores[],int-size)
在代码中,将playerNames
作为字符串而不是字符串数组传递
< >代码> GETLIN(CIN,PraveReal[index ]);代码> PrreNeWord(索引)是一个字符,因为 Prror Nords/COD>是一个字符串。 < P>我可能错了,因为我使用C++已经有很长时间了,但是你试过Cin?GETLIN而不是GETLIN?< /P>
错误:没有重载函数“getline”的实例与参数列表匹配参数类型为:(std::istream,char)
这意味着您要将单个字符值传递给getline()
,而不是整个字符串。您可以在以下位置执行此操作:
您将playernames
作为字符串变量而不是sting[]
数组进行传递。因此,当您执行playernames[index]
时,您正试图向函数传递单个字符值
根据main()
函数中的代码判断,您希望向函数传递字符串数组,而不仅仅是单个字符串
因此,请更改InputData()
致:
**@Pinky先告诉我答案:),但我想提供更多的细节,所以我发布了我的答案
另一件事,除了我想指出的主要问题之外,您应该将main()
函数的返回数据类型从void main()更改为int main()
是的,这是我做错的。非常感谢。我的荣幸:)@jackofblazeI关于我的getline的另一个问题有进一步的问题,我应该在这里提问还是开始一个新的问题?我只是在getline第一次运行后跳过任何用户输入,并一直这样做直到它通过所有的测试我认为你应该开始一个新的问题,因为你可以详细描述这个问题,吸引更多的人。@jackofblaze
getline(cin, playerNames[index])
int InputData(string playerNames, int scores[], int size)
int InputData(string playerNames[], int scores[], int size)