Search name(String Manipulation)

Desired Output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Ana
Ana is located at position 1

My output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search Ana
A is located at position 1...

Desired Output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Mina
The names is not found in the list.

Desired Output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Mina
M is located in

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
  #include<iostream>
using namespace std;

int main()
{
	char st1[20][100];
	char names;
	int n = 0, x;
	cout << "Enter n: ";
	cin >> n;
	cout << "Please input " << n << " names: ";
		for (int i = 0; i < n; i++)
			cin >> st1[i];
		for (int i = 0; i < n; i++)
			cout << st1[i] << endl;
	
	cout << "Enter name to search: ";
	cin >> names;
	cout << names << " is located at position ";
	for (int i = 0; i < n; i++){
		if (*st1[i] == names){
			cout << i + 1;
			x = 1;
		}
	}
	if (x == 0)
		cout << "The name " << names << "is not found in the list.";

	system("pause>0");
	return 0;
}
That is a rather large two-dimensional array, is that necessary? Consider using a vector, as it grows and shrinks during run-time to fit the amount of storage you TRUELY need! Also, you are trying to store a string of characters into a single char variable, change the names variable into a string variable, and everything should work correctly! If you need any further explanation, let me know!
Last edited on
Thank you! @FSM Worshipper :) My other concern is this:
Desired Output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Mina
The names is not found in the list.

Desired Output:
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Mina
M is located in
This reason it's not working is you are not initalizing x to anything, try initalizing it to 0. Instead of x being a int, try a boolean, and call it searchSuccessful and initialize it to false.
If you find it, change it to true!

1
2
3
4
5
6
7
8
bool searchSuccessful = false;

if ( !searchSuccessful )
{
		cout << "The name " << names << "is not found in the list.";
}


It is my personal preference to always use booleans in instances like this. They are much more clear a descriptive!
Last edited on
It outputs both of them.
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Ana
Ana is located in positionThe name Ana is not found in the list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cout << "Enter name to search: ";
	cin >> names;
	cout << names << " is located at position ";
	for (int i = 0; i < n; i++){
		if (st1[i] == names){
			cout << i + 1;

		}
	}
	
	bool searchSuccessful = false;

	if (!searchSuccessful)
	{
		cout << "The name " << names << " is not found in the list.";
	}

	system("pause>0");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool searchSuccessful = false; //declare this up with the rest of your variables at the beginning!


cout << "Enter name to search: ";
	cin >> names;
	for (int i = 0; i < n; i++){
		if (st1[i] == names){
			cout << i + 1;
                        cout << names << " is located at position "; // This was moved in here, we don't always want to see this!
                        searchSuccessful = true; //Need this so it doesnt execute both, if found, its true!

		}
	}
	

	if (!searchSuccessful)
	{
		cout << "The name " << names << " is not found in the list.";
	}

	system("pause>0");
	return 0;
}
Last edited on
How does that work for you?
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Mina
Mina is located in positionThe name Mina is not found in the list
Though if it's included in the list, it works.
Enter n: 2
Please input 2 names: Ana Ina
Ana
Ina
Enter name to search: Ana
Ana is located in position 1.
I edited last post, I moved the position announcement INSIDE the if statement, so it will only execute it when we find it.
Working correctly now?
Still the same
This isn't working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool searchSuccessful = false; //declare this up with the rest of your variables at the beginning!


cout << "Enter name to search: ";
	cin >> names;
	for (int i = 0; i < n; i++){
		if (st1[i] == names){
			cout << i + 1;
                        cout << names << " is located at position "; // This was moved in here, we don't always want to see this!
                        searchSuccessful = true; //Need this so it doesnt execute both, if found, its true!

		}
	}
	

	if (!searchSuccessful)
	{
		cout << "The name " << names << " is not found in the list.";
	}

	system("pause>0");
	return 0;
}
oh wait.. it now works!
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
#include<iostream>
#include <string>
using namespace std;

int main()
{
	char st1[20][100];
	string names;
	int n = 0;
	cout << "Enter n: ";
	cin >> n;
	cout << "Please input " << n << " names: ";
	for (int i = 0; i < n; i++)
		cin >> st1[i];
	for (int i = 0; i < n; i++)
		cout << st1[i] << endl;

	bool searchSuccessful = false; //declare this up with the rest of your variables at the beginning!


	cout << "Enter name to search: ";
	cin >> names;
	for (int i = 0; i < n; i++){
		if (st1[i] == names){
			cout << i + 1;
			cout << names << " is located at position "; // This was moved in here, we don't always want to see this!
			searchSuccessful = true; //Need this so it doesnt execute both, if found, its true!

		}
	}


	if (!searchSuccessful)
	{
		cout << "The name " << names << " is not found in the list.";
	}

	system("pause>0");
	return 0;
}

Thank you!
Glad I could help, I love sharpening my debugging skills and reading other peoples code! :)
I'm a total newbie so thank you for helping me ^^
Topic archived. No new replies allowed.