Hello,
I am having trouble implementing the load function below. It compiles, but I get garbage in the array and 0's in the vector.
The reason I created load() was to clean up main(). Do I need to open data.txt in main and then call a function to load the data structures?
#include "utility.h"
#include "lnklist.h"
#include <vector>
int main()
{
void printArray(int array[]); //need to combine
void printVector(vector<int> &v); //these two functions
void load(int array[], vector<int> &v);
int array[30]; //declare an array of type int with size 30.
vector<int> vectorOne(300); //declare a vector of type int with size 31.
/* int number;
int index = 0;
//=======================================================
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[index] = number;
vectorOne.at(index) = number;
index++;
}//end while in_stream >> number.
*/
printArray(array);
printVector(vectorOne);
load(array, vectorOne);
cout << "Done!" << endl;
return 0;
}
void load(int array[], vector<int> &v){
int number;
int index = 0;
//=======================================================
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[index] = number;
v.at(index) = number;
index++;
}//end while in_stream >> number.
}
void printArray(int array[]){
for (int i = 0; i < 30; i++)
{
cout << "Array: " << array[i] << endl;
}
}//function used to print data structures. See implementation below.
//Combine this with printArray using overloaded function
void printVector(vector<int> &v){
for (int i = 0; i < 30; i++)
{
cout << "Vector: " << v[i] << endl;
}
}//end printVector