3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by JTX
0

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

0

Pseudo:

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

0

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.

0

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?

0

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.