Hello,
This is my first time using vectors. The code below creates a vector of size 30, but when I run the program (which loads 30 integers into the vector), the last integer in my data.txt file does not load into the vector.
When I use a vector of size 31 it works fine. I am just curious why it does not work with 30. My data file has 30 integers, 0-29.
#include "utility.h"
#include "lnklist.h"
#include <vector>
int main()
{
int number;
int i = 0;
int array[29]; //declare an array of type int with size 30.
vector<int> vectorOne(29); //declare a vector of type int with size 30.
//=======================================================
ifstream in_stream;
in_stream.open("data.txt");
if(in_stream.fail( ))
{
cout << "unable to find input file"; //displays error message if file is not found
exit(1); //exits the program if file is not found
}//end if in_stream.fail( )
//=======================================================
while (in_stream >> number) //in_stream >> number returns a Boolean value of true if the operation has been successful.
{
//array[i] = number;
//cout << array[i] << endl; //test array in feed
vectorOne.at(i) = number;
cout << vectorOne.at(i) << endl;
i++;
}
cout << "Done!" << endl;
return 0;
}