8 Years
Discussion Span
Last Post by JTX

I want to put it in an integer array with a for loop. Your link doesn't really help.



Read first integer from text file
put it in Array[0]
Read second integer from text file
put it in Array[1]

With a for loop, it would look something like this:

for (i=0; i < sizeOfArray; i++)
   put integer into Array[i]

I don't know what to put inside of the for loop

Edited by JTX: n/a


How would assign a value to an element of the array?

Pretty much the same you assign a value to a plain int.

Your loop is fine, if you know exactly how many values are in the data file, and that number is the size of the array. Usually a better choice is a while loop in which the stopping conditions are not having any more room in the array or having run out of data to read, whichever comes first.


It works if you have the numbers written one per line. What if I wanted to read them from one line, where the numbers are separated by spaces?


try this:

int a, b, c;

cin >> a >> b >> c;

When this runs, type the following
1 2 3 <enter>

Does it work?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.