jackofblaze
jackofblaze

Reputation: 37

C++ Issue Using Getline

I'm trying to create a program in which the user can enter a series of player names and scores and read them back. However, I'm having trouble storing their input using getline. On getline in the InputData function, visual studio states, "Error: no instance of overloaded function "getline" matches the argument list argument types are: (std::istream, char)", and on ==, it says, "Error: operand types are incompatible ("char" and "const char *")". Here is my code:

#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;
}

Upvotes: 1

Views: 996

Answers (3)

BusyProgrammer
BusyProgrammer

Reputation: 2791

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

This means that you are passing a single char value to getline() instead of the entire string. You do this in:

getline(cin, playerNames[index])

You pass playernames as a string variable instead of a sting[] array. So, when you do playernames[index], you are trying to pass a single char value to the function.

Judging from the code in your main() function, you want to pass an array of strings to the function, not just an individual string.

Therefore, change the arguments of InputData()

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

To:

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

**@Pinky beat me to posting the answer first :), but I wanted to provide some more details, so I've posted my answer.

Another thing, aside from your main question that I wanted to just point out, you should change the return data type of your main() function to int main() from void main().

Upvotes: 1

Yash sharma
Yash sharma

Reputation: 3

i might be wrong because it's been a really long time since i've used c++, but have you tried cin.getline instead of just getline?

Upvotes: 0

Jiahao Cai
Jiahao Cai

Reputation: 1250

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

should be

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


In your code, you pass playerNames as a string instead of a string array.

In getline(cin, playerNames[index]); playerNames[index] is a char, because playerNames is a string.

Upvotes: 2

Related Questions