Reputation: 15
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);
}
Upvotes: 0
Views: 99
Reputation: 14659
The correct parameter would be an object of type Horse
. You can change your code to this. The below code seems to be just a snippet, I'm assuming it's part of a function body that you've defined.
i.name
in this case is of type std::string
, but your prototype of getName
takes a Horse
object, not a string.
for (Horse &i : Horses)
{
getName(i);
}
void getName(Horse &Nag)
{
cout << "Enter a horse's name: ";
getline(cin, Nag.name);
}
Upvotes: 0
Reputation: 9619
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.
Upvotes: 2