Anyways, I found two other problems with your function:
1 2 3 4 5
int number(const string o[], int n, string word)
{
int number; // Uninitialized -- you should set this to 0 first
for (int i = 0; i < n, i++;) // Should be: for (int i = 0; i < n; i++)
{
//#include "stdafx.h" //Dunno why this is here but it's not required.
#include <iostream>
#include <string>
usingnamespace std;
//Setting a default value for word and passing by reference for the lulz
/*
* Search an array of strings for input
*/
int number(const string o[], constint &length_of_array, string word = "word")
{
int number = 0;
/*
* Step through array to count number of instances of words
*/
for (int i = 0; i <= (length_of_array-1) ; i++)
{
if (o[i] == word)
{
number++;
}
}
return number;
}
int main()
{
string array_of_words[] = { "word", "word", "word", "boo", "word" };
cout << number(array_of_words, 5, "word");
return 0;
}
That was not the issue he defined the string array perfectly fine. The issue he had was he never initialized his count to 0 so when he incremented it was undefined behavior and that he put quotes around a variable name.
int number(const string o[], int n, string word)
{
int number;
for (int i = 0; i < n, i++;)
{
if (o[i] == "word")
{
number++;
}
}
return number;
}
int main ()
{
string o[5] = {"l", "t", "t", "word", "word"};
cout << number;
}
Line 17 prints the address of the function number. It does not call the function.