was looking over this program and im trying to figure out how to search for letters in an array instead of numbers, it works for numbers but how can i make it work for letters. please help…………… here is the code
#include <iostream>
using namespace std;
const int DECLARED_SIZE = 4;
void fillArray(int a[], int size, string& letter);
int search(const int a[], string letter, string target);
int main( )
{
int arr[DECLARED_SIZE]; string listletter; string target;
fillArray(arr, DECLARED_SIZE, listletter);
char ans;
int result;
do
{
cout << "Enter a letter to search for: ";
cin >> target;
result = search(arr, listletter, target);
if (result == -1)
cout << target << " is not on the list.\n";
else
cout << target << " is stored in array position "
<< result << endl
<< "(Remember: The first position is 0.)\n";
cout << "Search again?(y/n followed by Return): ";
cin >> ans;
} while ((ans != 'n') && (ans != 'N'));
cout << "End of program.\n";
return 0;
}
void fillArray(int a[], int size, string& letter)
{
cout << "Enter up to " << size << " letter.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
}
int search(const int a[], string numberUsed, string target)
{
int index = 0;
string run = "run";
bool found = false;
while ((!found)) // && (index < numberUsed))
if (target == run)
found = true;
else
index++;
if (found)
return index;
else
return -1;
}
I noticed you’re loading an int in fillArray. You need to use char if you want character input.