search an array to find if string is there or not

how to compare the string of the users input to a string put in by the user earlier?

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
 // Libraries
#include <iostream>
#include <cstring>
#include <string>

// standard library
using namespace std;

int main()
{

    int i = 0;
    char scientists [4][9]; 
    char value[9];

    cout << "enter 4 names: ";                // asks the user for names to be stored in 
    for (i = 0; i < 4; i++)                   // array
    {                                         // i used tom, dick, larry, and john
        cin >> scientists[i];
    }
    for (i = 0; i < 4; i++)                   // displays names in the array
    {
	cout << scientists[i] << endl;
    }

    cout << "enter name to look for in array"; // asking user for name to look for in array
    cin.ignore();
    cin.getline(value,9);

    if(strcmp(scientists[i],value)==0)         // comparing user input string to the
    {                                          // strings in array to see if it is there
        cout << "not here" << endl;
    }
    else
    {
        cout << "name is here " << endl;
        cout << "it is the " << i << " element of the array" << endl;
    }
		

    system("pause");
    return 0;
}

`my output was tom, dick, larry, and john
enter name to look for in array
greg
name is here 
it is in the 4 element of the array`
Is there a reason you don't want to use std::string? It would make it a lot easier, probably.
You have the sense of the comparison at line 30 wrong. strcmp() returns 0 when the strings are equal.

When you exit the loop at line 21, i is 4. It doesn't change so at line 30, you're comparing value to scientists[4]. In other words, you aren't comparing it to all the values in the array, just one (which isn't in the array anyway since legal indices of scientists are 0 to 3.
@Ganado i'm working through a book to learn as much as i can so i am the section that deals with character arrays and i'm trying to understand it as best as possible
@dhayden How would i get it to compare it to all of the elements within the array?
Put the if-else thing inside another for loop, like this:

1
2
3
4
5
6
7
8
int main()
{
//declare the arrays, get input, etc.
for (i = 0; i<4;i++)//this resets i back to 0 and loops again
{
//put the name search in here
    }
}
Last edited on
Thanks that helped a lot but, I need to figure out how to just show it once instead of displaying not here for each element.
When searching an array, you may have "found" more than once. But "not found" is a one-time only action, do that after the end of the loop, if no match was found.
@chervil so i would make a bool variable to save if its found or not and assign it true if its found and cout that after the loop
Yes, something like that would do. Set the flag to false before starting the search, and set it true when a match is found.

Another idea, if you only care about the very first result, use break to exit from the loop early when a match is found, and then test the final value of the subscript, to see whether or not the search reached the very end of the array.
So then would i use another if else statement, after the for loop that now contains the the string compare if else statement?
That all sounds a bit vague, yes you need some sort of if statement after the loop has completed, to detect the case where the item was not found.
I meant some like this

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
// Libraries
// standard library
using namespace std;

int main()
{
	int i = 0, position = 0;
	char name [4][9]; 
	char value[9];
	bool found = false;

	cout << "enter 4 names: ";
	for (i = 0; i < 4; i++)
	{
		cin >> name[i];
	}
	for (i = 0; i < 4; i++)
	{
		cout << name[i] << endl;
	}

	cout << "enter name to look for in array: ";
	cin.ignore();
	cin.getline(value,9);
	
	for(i = 0; i < 4; i++)
	{
		if(strcmp(name[i],value) == 0)
		{
			found = true;
			position = i + 1;
		}
		else
		{
			found = false;
		}
	
	}	
	if(found == true)                     // this is the if else i was talking about
	{
		cout << "The name is in the Array" << endl;
		cout << "It is in the " << position << " element of the Array" << endl;
	}
	else
	{
		cout << "Its Not Here" << endl;
	}
	system("pause");
	return 0;
}
Not exactly. That's just another variation of the same mistake. As I said previously,
Set the flag to false before starting the search


In fact, since you are storing the position like this, position = i + 1;, the bool found is redundant.

You could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    bool found = false;
    for (i = 0; i < 4; i++)
    {
        if (strcmp(name[i],value) == 0)
        {
            found = true;
            cout << "The name is in the Array" << endl;
            cout << "It is in the " << (i + 1) << " element of the Array" << endl;
        }
    }
    
    if (!found)     
    {
        cout << "Its Not Here" << endl;
    }


or instead, do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int position = -1;
    
    for (i = 0; i < 4; i++)
    {
        if (strcmp(name[i],value) == 0)
            position = i + 1;
    }    
    
    if (position > 0) 
    {
        cout << "The name is in the Array" << endl;
        cout << "It is in the " << position << " element of the Array" << endl;
    }
    else
    {
        cout << "Its Not Here" << endl;
    }


or another possibility:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for (i = 0; i < 4; i++)
    {
        if (strcmp(name[i],value) == 0)
            break;
    }    
    
    if (i < 4) 
    {
        cout << "The name is in the Array" << endl;
        cout << "It is in the " << (i + 1) << " element of the Array" << endl;
    }
    else
    {
        cout << "Its Not Here" << endl;
    }


Note: these various methods will all behave slightly differently. One finds all matches, another just the first match, the other just the last match.
Last edited on
Thank You so much, why do you set up the position variable to -1... i read it in the book but i don't understand the reason behind it. and when i tried the first way you sent it did the same thing as before... which is go through each element and prints out not here for each element until its found.
why do you set up the position variable to -1
For exactly the same reason as setting bool found = false;
it is something set before starting the loop, that means "not found". When the loop has ended, we can test this to see whether or not the item was found.
when i tried the first way you sent it did the same thing as before... which is go through each element and prints out not here for each element until its found.

you mean this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    bool found = false;
    for (i = 0; i < 4; i++)
    {
        if (strcmp(name[i],value) == 0)
        {
            found = true;
            cout << "The name is in the Array" << endl;
            cout << "It is in the " << (i + 1) << " element of the Array" << endl;
        }
    }
    
    if (!found)     
    {
        cout << "Its Not Here" << endl;
    }


That could not possibly print "not here" for each element, as the message "Its Not Here" is after the end of the loop.
O ok, thank you for your help... i'm really trying to learn and understand this stuff... I wish, had someone to teach it to me.
it will take time - and practice. Earlier in this thread I posted three different variations - then I started to worry. That's enough to overload anyone when dumped all in one go. It might also get you in a muddle. But when you have time it might be worthwhile to go through it all again, slowly.
@Cnewbie51, you may want to ask in the lounge forum for a tutor. Most questions in this forum are from people taking classes who need help with homework, so the answers tend to be vague. You would be better served by someone who can show you the code.
Topic archived. No new replies allowed.