Dreamerv3,
(It's a little early, so, please, bear with me...)
This allocates enough memory in the vector of five 'shorts'. (While a vector isn't, generally, hampered by memory constraints, there are times when it's advisable to set aside a specific amount of memory for a vector to avoid the overhead (time) of forcing the compiler to relocate the array to a different area on the 'free store'.)
'i' must be declared, regardless. In the FOR loop statement, 'i' serves merely as a counter, however, because 'i' is a variable, it must be declared with a type just as any other variable must be.
To this point, 'i' and Weight(5) are, as yet, unrelated to one another. Apples and oranges, if you will.
To your primary question, I've never seen alpha characters used as array subscripts in place of numerals. Can this even be done? Yes.
Code:
enum sub {a, b, c, d, ..., z};
As you may already be aware, the 'enum' statement creates user-defined types of these char's. The compiler will evaluate 'a' as 0, 'b' as 1, 'c' as 2, ..., 'z' as 25.
Now, would I recommend doing such a thing. Oh, Lord, no! Offhand, I'd be afraid to see what would happen!
Could you also do this?:
Code:
short a[5];
short b[5];
short c[5];
Yes, you could. This begs the question of whether, or not, however, you could then compile
Code:
cin >> Weight[a[i]]; // where i = 0 to 4 (your five additional values)
Seems possible, but compilers are funny animals.
Last question, (I think). In the 'cout' statement, you can manipulate 'i' by adding one, subtracting two, multiplying by three, whatever. You just can't reassign the value if you want the loop to remain intact.
That being said, an old BASIC trick, while searching an array sequentially, was to reassign the counter value to an arbitrarily high number within the FOR loop to force it to break out of the loop when a comparison evaluated to 'true'. Much along the lines of using 'break;' in C++ and accomplishing the same thing.
Code:
cout << "I'm going to ask you to type in five weights, in pounds." << endl;
for (i = 0; i < 5; i ++)
{
cout << "Please type in weight #" << i+21 << ": ";
cin >> Weight[i];
}
This displays, "Please type in weight #22:". 'i' remains whatever value it was.
Code:
cout << "I'm going to ask you to type in five weights, in pounds." << endl;
for (i = 0; i < 5; i ++)
{
cout << "Please type in weight #" << i+1 << ": ";
cin >> Weight[i];
i = 21; // 'i' now equals 21 and your loop is kaput!
}
This will display, "Please type in weight #1:", and then reassigns 'i' the value of 21. The test in your FOR will fail and the loop will end.
Whew...
-Skipper