calculator troubles

Well I'm trying to learn templates. So I thought I would make a calculator that you could type in like 10 and 1 or 1.646474 and 1.563 and it would use the correct data types. Is it possible? or do not understand what templates are for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>

using namespace std;

template <class A_Type> class Calculate
{
    public:

    A_Type NumOne;
    A_Type NumTwo;
    A_Type Answer;

    Calculate():NumOne(0), NumTwo(0), Answer(0){};

     A_Type Add()
    {
        Answer = NumOne + NumTwo;
    }

     A_Type Subtract()
    {
        Answer = NumOne - NumTwo;
    }

     A_Type Divide()
    {
        Answer = NumOne / NumTwo;
    }

     A_Type Multipy()
    {
        Answer = NumOne * NumTwo;
    }
};

int main()
{
    Calculate<int> calc;

    string Input;

    cout << "What operator would you like to perform?" << endl;
    cout << "Enter add, subtract, multiply, or divide" << endl;
    getline(cin, Input);

    if((Input != "add")&&(Input != "subtract")&&(Input != "divide")&&(Input != "multiply"))
    {
        cout << "Incorrect input. Exiting program" << endl;
        return 0;
    }

    cout << "Ok now enter two numbers to calculate" << endl;
    cin >> calc.NumOne >> calc.NumTwo;

    if(Input == "add")
    {
        calc.Add();
    }

    if(Input == "subtract")
    {
        calc.Subtract();
    }

    if(Input == "divide")
    {
        calc.Divide();
    }

    if(Input == "multiply")
    {
        calc.Multipy();
    }

    cout << "The answer is " << calc.Answer << endl;

    return 0;
}
No, for templates, types must be known at compile time. What you are wanting
is a runtime check.
Could you give an example because I'm confused.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <boost/lambda/lambda.hpp>

int main( int argc, char* argv[] ) {
    std::vector<int> v;   // This is now a vector of ints, nothing else

    for( int arg = 1; arg < argc; ++arg ) {
       try {
            v.push_back( boost::lexical_cast<int>( argv[ arg ] ) );
       } catch( const boost::bad_lexical_cast& ) {
            std::cout << "argument #" << arg << " is not an integer" << std::endl;
       }
   }

   std::cout << "The ints you specified on the command line were:" << std::endl;
   std::for_each( v.begin(), v.end(), std::cout << boost::lambda::_1 << ' ' );
}


So I've used a lot of advanced libraries to make this code as short as possible, and
chances are you aren't familiar with most of them. Anyway, a quick rundown:

boost::lexical_cast<int>() is a function that takes its argument and tries to convert
it to an integer (since I specified int. If I specified double instead, it would have tried
to convert it to a double).

If the conversion fails, boost::lexical_cast<> throws a boost::bad_lexical_cast
exception. The last line -- std::for_each -- is an advanced way to print out the
contents of the vector in a single line of code.

But see how with templates, I essentially had to decide at compile time that the program
would only recognize ints and store them.
Interesting
Topic archived. No new replies allowed.