Hi guys, I'm having a little trouble with understanding range for / vectors. I have a function for addition, the user can enter as many numbers to add until we hit 0 to exit and print. I use a vector to hold each number (for printing).
void addition()
{
usingnamespace std;
cout << " == Addition ==\n" << endl;
double dNum; // input for the current number
double dSum = 0; // sum total
vector<double> vStoredNums; // stores each input, used for when we print the total
// get the numbers
do
{
cout << "Enter number to add (0 to finish): ";
cin >> dNum;
dSum += dNum;
vStoredNums.push_back(dNum); // store the current num
} while (dNum != 0);
// print each "num +" = total
for (auto kkk : vStoredNums)
cout << vStoredNums[kkk] << " + "; // need to change so we don't get an empty +
cout << " = " << dSum;
cout << endl;
}
When I hit the 0 I'm crashing. Instead of just printing the total I wanted to use a vector to hold each addition, so we could do num + num + num... = dSum. Can anyone spot what is wrong with the range for?
Enter number to add (0 to finish): 1
Enter number to add (0 to finish): 2
Enter number to add (0 to finish): 3
Enter number to add (0 to finish): 0
2 + 3 + 0 + 1 + = 6
@vlad, thanks just kkk worked. There's too much different sytax to remember!!! :-)
@metalburr, when I run it without vlad's change and I hit the 0 I get a crash: "Debug Assertion Failed!" (.../vc/include/vector) line 1140. "Expression: vector subscript out of range".
do
{
cout << "Enter number to add (0 to finish): ";
cin >> dNum;
dSum += dNum;
vStoredNums.push_back(dNum); // store the current num
} while (dNum != 0);
for
1 2 3 4 5 6 7 8 9 10
do
{
cout << "Enter number to add (0 to finish): ";
cin >> dNum;
if ( dNum != 0 )
{
dSum += dNum;
vStoredNums.push_back(dNum); // store the current num
}
} while (dNum != 0);
@vlad thank you again. Frustratingly I couldn't even remember how to do it that way. Everytime I learn 10 new things I forget 5 things, it is really stressing me out. I would've done that code 2 weeks ago like second nature. :(
Learning all this new stuff like classes, iterators and vectors has made me almost completely forget simple things like control structures!
i dont think you understand rangebased for loops (no big deal i didnt either but they can be really simple) how it works:
for(iterator : container). for the iterator you usually just want it to have auto. the iterator points to the cell of the times you ran through the loop - 1. ie if youve gone through five times it will access scores[4]. when you want to use the container in the loop, you use the iterator. if you want to change it you make the iterator an & variable. so lets say i had this:
1 2 3 4
vector<int> myvec;
for(auto& i : myvec)
i->push_back(0);
see how i used i instead of myvec[i]? so for your thing up above
1 2 3 4 5 6 7 8
unsigned scores[10];
int j=0;
for(auto &i : scores)
{
i = j++;
cout<< i << endl;
}
I think I'm getting parts of it but I'm a little lost on just what we can do with the it. So if I'm correct, in this:
1 2 3 4 5 6 7 8
unsigned scores[10];
int j=0;
for(auto i : scores)
{
i = j++;
cout << i << endl;
}
Without the &, when I use i in the body i gets a copy (like copy by value?) and has no links to the element it got it from? i.e. you can't use member functions of whatever it was iterating? But i in the (...) will be ofcourse still be the iterator, separate from when we use i in the body?
I'm a bit lost at using it's properties, like in conditions, i.e.:
1 2 3 4
for (auto &it : vStoredNums)
cout << it;
if ( !it.end() )
cout << " + ";
like here I only want the + when another number is coming next so I want to check the iterator does have a number to follow before printing the + sign. This gets an error with or without &.