Integers into an int array ?!?

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
How do I get a file of numbers separated by spaces into an array of integers??
Assuming that there will be no more than 100 integers entered. 
This is the code I have so far. Please help!!
 
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{


in_stream2.open("gradefile.txt");
if (in_stream2.fail( ))
    {
       cout << "Input file opening failed.\n";
		exit(1);
	}

int grade_array[100];

for (int i= 0; i > 100; i++)
{
	in_stream2 >> grade_array[i];
	
}
Last edited on
Please always use code tag sfor your code - http://www.cplusplus.com/articles/jEywvCM9/

for (int i= 0; i > 100; i++)

You sure about this? You want your loop to run as long as i is less bigger than 100? Think you got it backwards.

for (int i= 0; i < 100; i++)
Thanks! Sorry about the code tags I'm new to this website.
Do you know how I might find the number of integers in a file?
Maybe you can do something like...

1
2
3
4
5
6
int integerCounter = 0;

while(in_stream2 >> grade_array[i])
{
        integerCounter++;
}


When you've read all the integers, the variable integerCounter should be equal to the number of integers in the file. I havent tested it though since I dont have files and stuff. Try it out.
oh and btw, this while loop has to replace your for-loop. This is another way of reading in all the integers.

Last edited on
Topic archived. No new replies allowed.