String checker problem

For some reason my program will only work if the string to check inputed first.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


void setContainer(string x[], int size) {

	for (int i = 0; i < size; i++) {

		cout << "Please enter strings you want to array: ";
		cin >> x[i];
	}

}

string containerChecker(string container[], string x, int size) {

	for (int i = 0; i < size; i++) {

		if (container[i] == x) {

			return container[i];
		}

		else return "Not found";

	}

}


int main()
{

	string container[5];
	string checker = "";

	setContainer(container, 5);

	cout << "What string would you like to check? ";
	cin >> checker;

	string confirm = containerChecker(container, checker, 5);

	if (confirm != "Not found") {

		cout << "The search have found, the word: " << "|" << confirm << "|";
	}
	else
		cout << "Not found";


    return 0;
}

The problem seems to be with the fact that you have passed an array by value to the function

void setContainer(string x[], int size).

That means that the operations going on inside this function are being performed on a copy of your array instead of on the "container" array you created in Main(). You aren't actually adding the strings to variable "container".

Not sure if you are comfortably with the standard template library, but there are a ton of useful containers there that could help with this type of operation. Using, say a std::set container, you can reduce the need for some of those loops in your code; they are also much easier to pass either by reference or by value. Here's what it would look like using std::set.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <set>  //  so we can use std::set

using namespace std;


void setContainer(set<string>& container, int size)       // pass a set of strings instead of an array
{
    string x;
	for (int i = 0; i < size; i++) {

		cout << "Please enter strings you want to array: ";
		cin >> x;
		container.insert(x);
	}
}

string containerChecker(const set<string>& container, string x)
{
    set<string>::const_iterator const_iter;   
    const_iter = container.find(x);             // set is a binary search tree that makes it easy to find stuff.  
    if (const_iter == container.end())
        return "Not Found";
    return *const_iter;
}


int main()
{

	set<string> container;       // make container a std::set object.  
	string checker = "";

	setContainer(container, 5);

	cout << "What string would you like to check? ";
	cin >> checker;

	string confirm = containerChecker(container, checker);

	if (confirm != "Not found") {

		cout << "The search have found, the word: " << "|" << confirm << "|";
	}
	else
		cout << "Not found";


    return 0;
}
Last edited on
The problem seems to be with the fact that you have passed an array by value to the function

Arrays cannot be passed by value.

Array parameters immediately undergo the implicit array-to-pointer conversion. This phenomenon is sometimes called "array decay".
Last edited on
Thanks for clarifying mbozzi.
@tNK, the logic in function containerChecker is wrong. You should only return "Not found" AFTER you have checked ALL possibilities (i.e. after the end of the for loop). There will be no "else" here.

Change your lines 27 - 29 from
1
2
3
	else return "Not found";

	}

to
1
2
3
	}    // end of for loop

	return "Not found";



Maybe you want to reconsider your code though. Since the most you are going to do with containerChecker is echo back the word you sent it, it seems more natural that this function should simply return a bool: true (if found) and false (if not found).
Last edited on
skindig123 wrote:
Using, say a std::set container, you can reduce the need for some of those loops in your code; they are also much easier to pass either by reference or by value.

Since, the order strings are stored here does not matter std::unordered_set can be used. If order is not needed it is usually best to use it instead of std::set. There may also be improved performance (though in this case not much).
Last edited on
Topic archived. No new replies allowed.