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>
usingnamespace 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];
}
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.