Guest User

Untitled

a guest
Jun 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdarg>
  3.  
  4. struct array_info
  5. {
  6. int num_values;
  7. int * address;
  8. };
  9. template <typename Any> void Swap(Any &a, Any &b);
  10. template <typename Any> void Swap(Any a[], Any b[], int n);
  11. template <typename Any> void show_array(const Any a[], const Any b[], int size);
  12.  
  13. array_info * build_info_array(int * array_name, int num_of_elements);
  14. bool check_cin(std::istream &);
  15. template <typename IN_PUT> bool doInput(IN_PUT &);
  16. enum doInput_options {CHECK_POS, CHECK_LENGTH};
  17. template <> bool doInput<int>(int &);
  18.  
  19. int main()
  20. {
  21. using namespace std;
  22. int nums = 0;
  23. while (true)
  24. {
  25. cout << "How many numbers will you input? ";
  26. if (!doInput(nums))
  27. continue;
  28. cout << "Please enter " << nums << " numbers... " << endl;
  29. int * input = new int[nums];
  30. for (int i = 0; i < nums; i++)
  31. doInput(input[i]);
  32. array_info * information = build_info_array(input, sizeof(input) / sizeof(int));
  33. cout << ((int *)information->address)
  34. delete information;
  35. delete [] input;
  36.  
  37.  
  38. }
  39. //array_info * pt[2];
  40. //for (int i = 0; i < 2; i++)
  41. //{
  42. //pt[0] = build_info_array(ar1, sizeof(ar1)/sizeof(int));
  43. //pt[1] = build_info_array(ar2, sizeof(ar2)/sizeof(int));
  44. //}
  45.  
  46. //Test this sucker...
  47. //cout << "Array #1: elements - " << *pt[0].num_values << " address - " << *pt[0].address;
  48. //cout << pt[0]->num_values;
  49. //cout << endl << pt[0]->address[0];
  50. //delete pt[0];
  51. //delete pt[1];
  52. return 0;
  53. }
  54.  
  55. array_info * build_info_array(int * array_name, int num_of_elements)
  56. {
  57. array_info * new_ar_inf = new array_info;
  58. new_ar_inf->address = array_name;
  59. new_ar_inf->num_values = num_of_elements;
  60. return new_ar_inf;
  61. }
  62.  
  63. bool check_cin(std::istream &obj)
  64. {
  65. if (!obj)
  66. {
  67. obj.clear();
  68. while(obj.get() != '\n')
  69. {
  70. continue;
  71. }
  72. std::cout << std::endl << "Invalid input, try again..." << std::endl;
  73. return false;
  74. } else
  75. return true;
  76. }
  77.  
  78. template <> bool doInput<int>(int &var) //explicit specialization
  79. {
  80. using namespace std;
  81. cin >> var;
  82. if (!check_cin(cin))
  83. {
  84. var = 0;
  85. return false;
  86. } else
  87. {
  88. //Call any additional check functions...
  89. return ((var < 0) ? false : true);
  90.  
  91.  
  92. //return true;
  93. }
  94. }
  95.  
  96.  
  97. template <typename Any> void Swap(Any &a, Any &b)
  98. {
  99. Any temp;
  100. temp = a;
  101. a = b;
  102. b = temp;
  103. }
  104.  
  105. template <typename Any> void Swap(const Any a[], const Any b[], const int n)
  106. {
  107. Any temp;
  108. for (int i = 0; i < n; i++)
  109. {
  110. temp = a[i];
  111. a[i] = b[i];
  112. b[i] = temp;
  113. }
  114. }
  115.  
  116. template <typename Any> void show_array(Any a[], Any b[], int n)
  117. {
  118. return;
  119. }
Add Comment
Please, Sign In to add comment