/*
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>
usingnamespace 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;
}
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..
/*
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>
usingnamespace 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;
}
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?
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...
#include <iostream>
usingnamespace 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??