Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Creates a template for the ability to work with vars of any type
  6. template <class T>
  7.  
  8. //sum function
  9. T sum(T a, T b)
  10. {
  11. try {
  12. if (typeid(a).name() == "String" || typeid(b).name() == "String") {
  13. throw 1;
  14. }
  15.  
  16.  
  17. }
  18. catch (...) {
  19. cout << "Error #1: Can't use a string as a variable. ";
  20. }
  21. return a + b;
  22. }
  23.  
  24. template <class T>
  25.  
  26. //Getting input of any type here:
  27. T input(string type) {
  28.  
  29. switch (type) {
  30. case float:
  31. cin >> float a;
  32. return a;
  33. break;
  34.  
  35. case char:
  36. cin >> char a;
  37. return a;
  38. break;
  39. default:
  40. cout << "variable not specified." << endl;
  41. cin >> int a;
  42. }
  43. }
  44.  
  45. //main function
  46. int main()
  47. {
  48.  
  49. float a = input("float");
  50. float b = input("float");
  51.  
  52. auto result = sum(a, b);
  53. ;
  54.  
  55. cout << result <<endl;
  56. cout << "Press to exit" << endl;
  57. cin >> b;
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement