Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //Creates a template for the ability to work with vars of any type
- template <class T>
- //sum function
- T sum(T a, T b)
- {
- try {
- if (typeid(a).name() == "String" || typeid(b).name() == "String") {
- throw 1;
- }
- }
- catch (...) {
- cout << "Error #1: Can't use a string as a variable. ";
- }
- return a + b;
- }
- template <class T>
- //Getting input of any type here:
- T input(string type) {
- switch (type) {
- case float:
- cin >> float a;
- return a;
- break;
- case char:
- cin >> char a;
- return a;
- break;
- default:
- cout << "variable not specified." << endl;
- cin >> int a;
- }
- }
- //main function
- int main()
- {
- float a = input("float");
- float b = input("float");
- auto result = sum(a, b);
- ;
- cout << result <<endl;
- cout << "Press to exit" << endl;
- cin >> b;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement