I dont know where to go from here TEMPLATES

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
/*
Write a template that accepts an argument and returns its absolute value. 
The absolute entered by the user, then return the total. The argument sent
into the function should be the number of values the function is to read. 
Test the template in a simple driver program that sends values of various
types as arguments and displays the results.
*/

#include <iostream>

using namespace std;

template <class integertemplate>
integertemplate totalint (integertemplate integers)
{
	cout << "How many integer values do you wish to total? ";
	cin >> integers;
	
}
template <class doubletemplate>
doubletemplate totaldouble (doubletemplate doubles)
{
	cout << "How many double values do you wish to total? ";
	cin >> doubles;
}

int main ()
{

	system("pause");
	return 0;
}
what are you having trouble with? we wont do your homework for you
i don't have any homework i am taking text documents from the web to learn c++... anyways i don't quite understand how to use templates yet. I am self taught, so i miss a lot of important things i guess.
So..
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
/*
Write a template that accepts an argument and returns its absolute value. 
The absolute entered by the user, then return the total. The argument sent
into the function should be the number of values the function is to read. 
Test the template in a simple driver program that sends values of various
types as arguments and displays the results.
*/

#include <iostream>

using namespace std;


template <class integertemplate>
integertemplate totalint (integertemplate integers)
{
	cout << "How many integer values do you wish to total? ";
	cin >> integers;

	for(int i = 0; i < integers; i++;)
	{
	cout << "Please enter an integer:";
	}

	cout << "The total is: " << totalint() <<endl;
	
}
template <class doubletemplate>
doubletemplate totaldouble (doubletemplate doubles)
{
	cout << "How many double values do you wish to total? ";
	cin >> doubles;

	for(int i = 0; i < integers; i++;)
	{
	cout << "Please enter an integer:";
	}

	cout << "The total is: " << totaldouble() <<endl;
}

int main ()
{
	

	system("pause");
	return 0;
}


Is this reasonable?
Last edited on
not at all... you do know the point of templates right?
i believe if i can understand this e-book, (which i doubt) they are used to store multiple values or something, in a single variable without declaring a data type?
Last edited on
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

#include <iostream>

using namespace std;

template <class integertemplate>
integertemplate add (integertemplate n1, integertemplate n2, integertemplate n3)
{
	integertemplate result;

	result = n1+n2+n3;

	return result;	
}
template <class doubletemplate>
doubletemplate totaldouble (doubletemplate doubles)
{
	
}

int main ()
{
	cout << add(5,5,5);

	system("pause");
	return 0;
}


So i would assume this is a way to get started from a template
Last edited on
no... that is not how you use templates at all... that is an array type deal...
1)dont use using namespace std;
2) i wouldnt end template names with template, cause its a bit redundant.
3) dont use system
4) read this: http://www.cprogramming.com/tutorial/templates.html
Well i read that whole tutorial series, but in my compiler i use system pause as a quick fix, just so i can see the output in cmd. I have gotten pretty far, although why cant i use += to add my results? I assume number is just updating after the next integer is entered in...

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
#include <iostream>

using namespace std;


template <class integertemplate>
integertemplate add (integertemplate number)
{
	integertemplate result;

	result += number;

	return result;	
}


int main ()
{
	
	int numInts;
	int integerIn;

	cout << "How many integer values do you wish to total: ";
	cin >> numInts;
	for(int i = 0; numInts > i; i++)
	{
		cout << "enter a value: ";
		cin >> integerIn;
	}

	cout << add(integerIn);

	cout << "How many double values do you wish to total: ";


	system("pause");
	return 0;
}


It states uninitialized local variable result is used: even if i initialize it to 0, it still wont print the total. I assume i need to define an array in my template??
Last edited on
Finished!
Topic archived. No new replies allowed.