gpt4 book ai didi

使用 Getline 的 C++ 问题

转载 作者:行者123 更新时间:2023-11-30 02:26:10 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,用户可以在其中输入一系列玩家姓名和分数并读回它们。但是,我在使用 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;
}

最佳答案

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

应该是

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


在您的代码中,您将 playerNames 作为字符串而不是字符串数组传递。

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

关于使用 Getline 的 C++ 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com