/* This function takes in a reference of the input file object(ifstream) and the container used for storing all the values in our text file. It then inputs a whole line and dumps it into our container line by line, value by value */
void put_into_vector( ifstream& ifs, vector<int>& v )
{
// String to store our file input string
string s;
// Extracts characters from the input sequence until a delimited is found
getline( ifs, s );
// Input string stream class to modify the strings
istringstream iss( s );
// Skip all the white spaces. If not skipped then it would input just the first character in the string.
iss >> skipws;
// Function to check if stream's error flags (eofbit, failbit and badbit) are set.
if(iss.good())
{
// Copies elements within the specified range to the container specified.
copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
}
}
void get_value(vector<int>& v, int start, int end)
{
if(start == end)
{
sum = sum + v[start];
get_value(v,start,start+1);
}
if(v[start] > v[end])
{
sum = sum + v[start];
get_value(v,start,start+1);
}
if(v[start] < v[end])
{
sum = sum + v[end];
get_value(v,end,end+1);
}
}
int main()
{
vector<int> triangle_array[4];
ifstream ifs("numbers.txt");
for(int i = 0; i < 4; i++)
{
put_into_vector(ifs, triangle_array[i]);
}
int row = 0;
get_value(triangle_array[row], 0, 0);
system("PAUSE");
return 0;
}
This is what I am trying to achieve : My vector gets populated with the values I have in my text file which are
5
6 3
2 9 6
0 7 3 1
When my get_value function gets called with the initial parameters it checks if start == end which is correct since there is only one element in the beginning. This should add up and update my sum variable and call the next vector ie
vector[1] which points to 6 and 3.
I am not able to do this. I would really appreciate any feedback and comments on
how I should accomplish this.