Ah, I see the problem.
On line 52 you are assuming something about the input with (next >= 0)
. (According to the instructions it should be (next > 0)
.)
If you wish to use a semaphore item to terminate input, it must be passed as argument to the function.
However, a better idiom is to forget the semaphore stuff. Users consider it unnatural. Rather, accept a blank line as end of input. For this you'll need to #include <string> and <sstream>.
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;
...
template<class T>
void fill_array(T values[], int size, int& number_used, const string& itemname );
...
int main()
{
...
fill_array(int_values, SIZE_DECLARED, number_used, "integer");
fill_array(double_values, SIZE_DECLARED, number_used, "floating-point number");
fill_array(char_values, SIZE_DECLARED, number_used, "character");
...
}
template<class T>
void fill_array(T values[], int size, int& number_used, const string& itemname )
{
T next, search_value;
int index = 0, search_result;
cout << "Enter a maximum of " << size << ' ' << itemname << "s: \n";
cout << "Press ENTER on a blank line to end.\n";
string s;
while (getline( cin, s ) && !s.empty() && (index < size))
{
stringstream ss( s );
while ((ss >> next) && (index < size))
{
values[index] = next;
index++;
}
}
number_used = index;
cout << "Enter a " << itemname << " to search for: ";
cin >> search_value;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
search_result = search_array(values, number_used, search_value);
display_result(search_value, search_result);
}
There are a …