| | |
Template<class T> to partially fill array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 8
Reputation:
Solved Threads: 0
I need to create a function that uses a template to partially fill any array and from there search for a value in the array and return its position if found. The program below finds the values for double and int but I do know how to partially fill the array with string or char values. Could someone please help.
C++ Syntax (Toggle Plain Text)
//This is the file SearchArrayValue.cpp //Searches a partially filled array for a number and returns the position of the number if found and //-1 if not #include <iostream> const int SIZE_DECLARED = 10; template<class T> void fill_array(T values[], int size, int& number_used); //Pre condition: //Post condition: number_used is the number of elements used in the array //The array is filled with non negative number from values[0] to values[number_used - 1] //Values will be entered from the keyboard template<class T> T search_array(const T values[], int number_used, T search_value); //Pre condition: array values is filled with <= number_used values //Post condition: search_value will be searched in the array and if found the position will be returned //otherwise -1 will be returned template<class T> void display_result(T search_value, int search_result); //Pre condition: //Post Condition: The position of the searched item in the array is displayed if it was found //otherwise the appropriate message is returned. using namespace std; int main() { int number_used; int int_values[SIZE_DECLARED]; double double_values[SIZE_DECLARED]; char char_values[SIZE_DECLARED]; fill_array(int_values, SIZE_DECLARED, number_used); fill_array(double_values, SIZE_DECLARED, number_used); fill_array(char_values, SIZE_DECLARED, number_used); cin.get();//To keep console window open cin.get();//To keep console window open return 0; } template<class T> void fill_array(T values[], int size, int& number_used) { T next, search_value; int index = 0, search_result; char limit = '.'; cout << "Enter a maximum of " << size << " non negative values: \n"; cout << "(Enter a 0 to stop entry) \n"; cin >> next; while ((next >= 0)&&(index < size))//||((next != limit)&&(index < size)) { values[index] = next; index++; cin >> next; } number_used = index; cout << "Enter number/char to search for: "; cin >> search_value; search_result = search_array(values, number_used, search_value); display_result(search_value, search_result); } template<class T> T search_array(const T values[], int number_used, T search_value) { int index = 0; bool found = false; while ((!found) && (index < number_used)) if (search_value == values[index]) found = true; else index++; if (found) return index; else return -1; } template<class T> void display_result(T search_value, int search_result) { if (search_result == -1) cout << "The number " << search_value << " is not in the list" << endl << endl; else cout << "The number " << search_value << " was first found at values[" << search_result << "] of the array" << endl << endl; }
Last edited by edman; Jul 17th, 2008 at 4:31 pm.
Ah, I see the problem.
On line 52 you are assuming something about the input with
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>.
There are a few other things I dislike (like accessing cin and cout directly in a template function, or having the entry function call the search and display functions...), but those aren't critical (atm).
Hope this helps.
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>.
C++ Syntax (Toggle Plain Text)
#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 few other things I dislike (like accessing cin and cout directly in a template function, or having the entry function call the search and display functions...), but those aren't critical (atm).
Hope this helps.
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ error C2676
- Next Thread: c++ game help(continued)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






