Cannot call a created Function in body of code.

Hey I've created a function and I'm unable to use it in the body of the code. I have no idea what's going on. Good luck. Thanks.

#include <iostream>
#include <string>

using namespace std;

double showarray(int userarray[], int length)
{
for (int count = 0; count <= length; count++)
{
cout << userarray[count] << " " << endl;
}
return 0;
}

int main(int argc, const char * argv[])
{
string books[32] = { "Harry Potter", "Fictional Stuff", "Fahrenheit 451"};

cout << "Main Menu" << endl;

cout << "Display All Books(type display)" << endl;


string option;
cin >> option;

if (option == "display")
{
showarray(books, 32);
}
}
There's a few things wrong with this code.

1. ( The fix ). You pass books( an array of strings ) to a function accepting ( int, int ). You're passing ( string, int ).

2. Your for-loop in the function runs out of bounds of the array! count <= length. Length = 32, the array indices are 0-31.

3. You don't actually save the return of the function, so why not make it void. Then no return is necessary. Not really a problem, more of a "Cleaner code" option.

Hope these help (:
Last edited on
Thanks a ton for helping and being friendly, that worked great. I feel kinda stupid now, but whatever, I'm learning.
No problem. And don't worry. I still make mistakes, everyone does! That's why there's a debug feature! lol (:
Topic archived. No new replies allowed.