So I'm practicing for a test that is coming up, and I'm stumped on one of my questions. This is a two part question, I'll include both questions first.
1) Create a function that takes in an array of strings and returns a double. This double will represent the average length of all the strings.
2) Create a MAIN function that asks the user for 10 strings. Once the array is filled, call the function from Question 1 to calculate the average length of the strings and output the result to the console.
Here is my code so far:
void avg(std::string averages[], int size) {
double sum = 0;
for(int index = 0; index < size; index++) {
sum += averages[index]; // Error here, invalid operands to binary expression?
}
double retVal = sum / size;
return retVal; //**I am getting an error here: void function avg should not return a value
}
int main() {
std:: string myArray[10];
std:: cout << "Please enter 10 strings...";
for(int i = 0; i < 10; i++) {
std::cin >>myArray[i];
A void function CANNOT return anything. Change void to double. And,hopefully, somewhere you'll say that the variable, size, is equal to 10, or whatever the size of your myArray[], ends up being, otherwise, you'll have another error. When you initialized sum in the function, don't set it as an int, but the double. double sum = 0.0;