range for

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).

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
void addition()
{
	using namespace 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?
Last edited on
what do you mean crashing?

it works for me:
== Addition ==

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

Use


1
2
	for (auto kkk : vStoredNums)
		cout << kkk  << " + "; // need to change so we don't get an empty + 


instead of

1
2
	for (auto kkk : vStoredNums)
		cout << vStoredNums[kkk] << " + "; // need to change so we don't get an empty + 

@metulburr

it works for me


You are a happy man!:)
@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".

Also I would substitute this loop

1
2
3
4
5
6
7
	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!
Last edited on
Rather then create a new topic, I have a similar misunderstanding:

1
2
3
4
5
6
unsigned scores[10] = {0};
for (auto x : scores)
{
    x = x; // x element = x's value, i.e. element 0 = 0, 1 = 1...
    cout << scores[x];
}


I thought this would work like the vectors. I'm trying to use the range for to fill each element with it's element position.

Everything just gets filled with 0s. I thought x would be the first element (i.e hold 0)? I was looking for this equivalent:

1
2
3
4
5
6
unsigned scores[10] = {0};
for (int kkk=0; kkk < 10; kkk++)
{
    scores[kkk] = kkk;
    cout << scores[kkk];
}

Last edited on
In this code snip you at first initialized all elements of the array by zero.

1
2
3
4
5
6
unsigned scores[10] = {0};
for (auto x : scores)
{
    x = x; // x element = x's value, i.e. element 0 = 0, 1 = 1...
    cout << scores[x];
}


So what should be changed if a value is assigned to itself? I think that nothing shall be changed.

By the way this statement

for (auto x : scores)

creates copies for all elements of the array. If you want deal with original elements you should write

for (auto &x : scores)
Last edited on
closed account (Dy7SLyTq)
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 &.
Topic archived. No new replies allowed.