Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdarg>
- struct array_info
- {
- int num_values;
- int * address;
- };
- template <typename Any> void Swap(Any &a, Any &b);
- template <typename Any> void Swap(Any a[], Any b[], int n);
- template <typename Any> void show_array(const Any a[], const Any b[], int size);
- array_info * build_info_array(int * array_name, int num_of_elements);
- bool check_cin(std::istream &);
- template <typename IN_PUT> bool doInput(IN_PUT &);
- enum doInput_options {CHECK_POS, CHECK_LENGTH};
- template <> bool doInput<int>(int &);
- int main()
- {
- using namespace std;
- int nums = 0;
- while (true)
- {
- cout << "How many numbers will you input? ";
- if (!doInput(nums))
- continue;
- cout << "Please enter " << nums << " numbers... " << endl;
- int * input = new int[nums];
- for (int i = 0; i < nums; i++)
- doInput(input[i]);
- array_info * information = build_info_array(input, sizeof(input) / sizeof(int));
- cout << ((int *)information->address)
- delete information;
- delete [] input;
- }
- //array_info * pt[2];
- //for (int i = 0; i < 2; i++)
- //{
- //pt[0] = build_info_array(ar1, sizeof(ar1)/sizeof(int));
- //pt[1] = build_info_array(ar2, sizeof(ar2)/sizeof(int));
- //}
- //Test this sucker...
- //cout << "Array #1: elements - " << *pt[0].num_values << " address - " << *pt[0].address;
- //cout << pt[0]->num_values;
- //cout << endl << pt[0]->address[0];
- //delete pt[0];
- //delete pt[1];
- return 0;
- }
- array_info * build_info_array(int * array_name, int num_of_elements)
- {
- array_info * new_ar_inf = new array_info;
- new_ar_inf->address = array_name;
- new_ar_inf->num_values = num_of_elements;
- return new_ar_inf;
- }
- bool check_cin(std::istream &obj)
- {
- if (!obj)
- {
- obj.clear();
- while(obj.get() != '\n')
- {
- continue;
- }
- std::cout << std::endl << "Invalid input, try again..." << std::endl;
- return false;
- } else
- return true;
- }
- template <> bool doInput<int>(int &var) //explicit specialization
- {
- using namespace std;
- cin >> var;
- if (!check_cin(cin))
- {
- var = 0;
- return false;
- } else
- {
- //Call any additional check functions...
- return ((var < 0) ? false : true);
- //return true;
- }
- }
- template <typename Any> void Swap(Any &a, Any &b)
- {
- Any temp;
- temp = a;
- a = b;
- b = temp;
- }
- template <typename Any> void Swap(const Any a[], const Any b[], const int n)
- {
- Any temp;
- for (int i = 0; i < n; i++)
- {
- temp = a[i];
- a[i] = b[i];
- b[i] = temp;
- }
- }
- template <typename Any> void show_array(Any a[], Any b[], int n)
- {
- return;
- }
Add Comment
Please, Sign In to add comment