#include<iostream>
usingnamespace 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!
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!
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
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;
}
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;
}
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.
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;
}
#include<iostream>
#include <string>
usingnamespace 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;
}