array of strings function

Feb 19, 2014 at 1:12am
-solved thanks-
Last edited on Feb 19, 2014 at 4:34am
Feb 19, 2014 at 1:38am
Try replacing
if (o[i] == "word")
with
if (o[i] == word)
and see if that helps.
Feb 19, 2014 at 1:43am
--
Last edited on Feb 19, 2014 at 4:34am
Feb 19, 2014 at 1:48am
What's count?

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++)
    {
Feb 19, 2014 at 1:58am
You're doing it pretty wrong. You're comparing a char type with a string literal, which will lead to errors. You could do something like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
    std::string str = "word-word-word-word-word-not w-o-r-d";
    std::size_t size = 0;

    int found = 0;

    while((size = str.find("word",size)) != std::string::npos)
        found++,size++;

    std::cout << found << std::endl;

    return 0;
}
Feb 19, 2014 at 2:14am
@long double main
Thanks, I made those two corrections. The output is similar nonetheless. Count changed to number.
Feb 19, 2014 at 2:25am
You're comparing a char type with a string literal


He has an array of strings not an array of characters.
Feb 19, 2014 at 2:41am
He has an array of strings not an array of characters.

Yes, i didn't noticed that, my mistake.
Feb 19, 2014 at 2:57am
closed account (zybCM4Gy)
Please explain to me what n is supposed to be doing?
Feb 19, 2014 at 3:04am
Please explain to me what n is supposed to be doing?

He solved this I am pretty sure of. Anyways though n is for the number of elements there are in the array. otherwise you wouldn't know when to stop.
Feb 19, 2014 at 3:04am
Please explain to me what n is supposed to be doing?
n is the size of the array.
Nothing wrong there...
Feb 19, 2014 at 3:13am
closed account (zybCM4Gy)
Yeah I figured that >.<;;;

Looks to me like you can't define a string array in that manner >.>;;;

You know what. INIT THE FUDGING number variable. *whacks C++ with a hammer*. Can't believe that was the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//#include "stdafx.h" //Dunno why this is here but it's not required. 
#include <iostream>
#include <string>
using namespace 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[], const int &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;
}


Last edited on Feb 19, 2014 at 3:21am
Feb 19, 2014 at 3:16am
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.
Feb 19, 2014 at 3:16am
In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

Feb 19, 2014 at 3:22am
closed account (zybCM4Gy)
That's...true but it wasn't the problem.

int number was initing to some really freaky number all on its very own >_>;; (see my earlier post)

@giblit

Perhaps, but it was still fun to debug :D
Last edited on Feb 19, 2014 at 3:23am
Feb 19, 2014 at 4:39am
Yes that was the problem, the cout was not calling the function.
Topic archived. No new replies allowed.