Incrementing array indexes in for loop.

R

Roger

Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop
 
A

Aggro

Roger said:
Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

#include <vector>

std::vector<int> storage;
storage.push_back( 1 );
storage.push_back( 2 );
storage.push_back( 3 );
 
J

Jim Langston

Roger said:
Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop

Best use is std::vector.
Untested code:

#include <vector>
#include <iostream>

int main()
{
std::vector Data;

int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );

return 0;
}
 
J

Jim Langston

Jim Langston said:
Best use is std::vector.
Untested code:

#include <vector>
#include <iostream>

int main()
{
std::vector Data;

std::vector<int> Data;

Told you it was untested :/ My bad.
 
C

Chris Sorenson

Roger said:
How would I output the contents of the vector onto the screen?

std::vector<int>const_iterator int_iter;

for (int_iter = Data.begin(); int_iter != Data.end(); int_iter++)
std::cout << (*int_iter) << std::endl;

(don't forget to #include <iostream>)
 
J

Jerry Coffin

[ ... ]
How would I output the contents of the vector onto the screen?

There are quite a few ways. Here's one possibility:

std::copy(Data.begin(), Data.end(),
std::eek:stream_iterator<int>(std::cout, "\t"));

The "\t" governs what's used to terminate each item being copied -- if
(for example) you wanted each number on its own line, you could use
"\n" instead.
 
J

John Harrison

How would I output the contents of the vector onto the screen?

The traditional approach works too

for (int i = 0; i < Data.size(); ++i)
cout << Data << '\n';

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top