I am trying to make a function template that takes a "vector<T> v" as the parameter, and returns the total of all values in "v". It is supposed to be able to add ints, doubles, or strings, but I am having an issue.
So, when I try to run it (it compiles fine), I get this result:
**** VECTOR PROGRAM MENU ***********
* *
* Choose a data type *
* for your vector: *
* *
* 1. Integer *
* 2. Double *
* 3. String *
* 0. Exit *
* *
************************************
Enter option number.
: 1
How many integers do you want?
: 1
Enter an integer to go into the vector.
: 1
The total is: 91759646
Have a nice day!
template <typename T>
T accum (vector <T> v)
{
T total;
T x;
if (v.empty())
{
return 0;
}
for (size_t i = 0; i < v.size(); ++i)
{
// I think the error is here, but I cna't figure out what it is
x = v.at(i);
total = total + x;
}
return total;
}
/*----------Main function--------------*/
/*=====================================*/
int main (int argc, charconst *argv[])
{
int option, size;
//Accumulator adder;
cout << endl
<< " **** VECTOR PROGRAM MENU *********** \n"
<< " * * \n"
<< " * Choose a data type * \n"
<< " * for your vector: * \n"
<< " * * \n"
<< " * 1. Integer * \n"
<< " * 2. Double * \n"
<< " * 3. String * \n"
<< " * 0. Exit * \n"
<< " * * \n"
<< " ************************************ \n"
<< "\nEnter option number. \n: ";
cin >> option;
switch (option)
{
case 1:
{
vector <int> list {};
int number, total = 0;
cout << "How many integers do you want? \n: ";
cin >> size;
for (size_t i = 0; i < size; ++i)
{
number = 0;
cout << "Enter an integer to go into the vector. \n: ";
cin >> number;
list.push_back (number);
}
//adder.accum (list);
total = accum (list);
cout << "The total is: " << total << endl;
}
break;
}
}
This isn't all the code, just the part I'm testing as of right now. If you want the rest, feel free to ask!
I believe that the error is in line 14, inside the function template, however I could be wrong. Also, the output number changes each time I run it. Maybe it has something to do with the address? Although the output wasn't in hexadecimal...I'm stumped.
Any help would be appreciated.
Thanks!
max
Yea I can't figure out how the heck to initialize "total". I tried "" but that didn't work, I tried "0", that didn't work either, I tried total = 0; but that didn't work either!!
And sorry about calling it "list" but that was just what came off the top of my head.
In Python they're called lists, in Java they're called ArrayLists, in C# they're called Lists, in C++ they're called vectors... my personal opinion is that it's not a big deal that the variable is called 'list', as long as I know what type of collection it actually is.
Yea I can't figure out how the heck to initialize "total".
If you must, use value-initialization: T total{};
A more flexible (generic) option is to pass "total" as an argument by value.
1 2 3 4 5
template <typename T> T accum(std::vector<T> v, T acc)
{
for (autoconst& elt: v) acc = acc + elt;
return acc;
}
A similar algorithm exists in the standard library as std::reduce, but if you require that elements are added in order, std::accumulate has you covered.
*facepalm* Why didn't I think of that. I hate OCD sometimes...makes you concentrate on some things so hard you completely forget about others. Part of it's because I use c++03 instead of c++11 (it's what I learned), but I really should have thought of that.
But thanks so much! I'll try that out and see if it works!
max
Edit:
It worked! Thanks @mbozzi, @ne555!
Oh, and just to show how dumb I can get, would you believe that I spent 15 minutes trying to figure out why setprecision() wasn't working, only to find that I was misspelling it? Just another of those little things.
Oh, and just to show how dumb I can get, would you believe that I spent 15 minutes trying to figure out why setprecision() wasn't working, only to find that I was misspelling it? Just another of those little things.
Yeah, I bet most of us have done that (or something similar) at one time or another.
Hmm...I think it would be fun to start a discussion in the Lounge about dumb things we've done while programming.