brisk172 brisk172 - 2 months ago 6
C++ Question

C++ Function Coding

I just started learning functions. I'm trying to write this code with using a function to have a user input the names. I can't figure out what the argument should be. I thought I could put in getName(i.name) passing an array into it, but I'm doing it wrong. What should the correct parameter be?

void getName(Horse &); //function prototype
struct Horse
{
string name;
}
array<Horse, horseCount> Horses;
for (Horse &i : Horses)
{
getName(i.name);
}
void getName(Horse &Nag)
{
cout << "Enter a horse's name: ";
getline(cin, Nag.name);
}

Answer

You are passing a std::string to getName() when it actually expects Horse&.

You must call it as getname(i) to actually pass the Horse object.