Hello... I am trying to read the contents of a file into a Vector Array, which works, however I cannot get the array to print the Array in a reverse order. This is the data that I am reading from:
This is the actual program:Code:S 10 10 10 20 22 34 C -3 44 5 9 -6 11 22 19 44 7 354 -12 231 15 C 98 124 999 25 288 16 29 43 58 12 11 S 22 31 16 7 88 1297 41
Any help would be greatly appreciated.Code:#include <iostream> #include <fstream> #include <vector> #include <string> #include <numeric> using namespace std; void CFunc( istream & inf, ofstream & outf ) { int i, mean = 0, count = 0, sum = 0; vector<int> V(1,0); for( i = 0; i < V.size(); i++ ) { cout << " "; while(inf.peek() != '\n') { inf >> V[i]; V.push_back(i); //cout << V[i] << " "; //Reading from here works with cout. sum = sum + V[i]; count++; } } //cout << "vsize =" << V.size() - 1 << endl; //i = V.size() - 1; cout << "i = " << i << endl; for( i = V.size() - 1; i >= 0; i-- ) // This is the loop that I am using to try to read the V vector array in reverse order. { cout << V[i] << " "; } // The result of this produces Zeros for some reason. mean = sum / count; cout << endl << " Mean = " << mean << " " << " Count = " << count << endl; outf << endl << " Mean = " << mean << " " << " Count = " << count << endl; cout << endl; } void Scramble( istream & inf, ofstream & outf ) { int i; char aLine[60]; inf.getline(aLine, 60); cout << " Original Line: " << aLine << endl; outf << " Original Line: " << aLine << endl; for( i = 0; i <= 59; i++ ) { if( aLine[i] == '\n') { outf << " Scrambled Line: " << aLine << endl; cout << " Scrambled Line: " << aLine << endl; cout << endl; break; } if( aLine[i] == '0' || aLine[i] == '1') aLine[i] = 'A'; if( aLine[i] == '2' || aLine[i] == '3' || aLine[i] == '4') aLine[i] = 'B'; if( aLine[i] == '5' || aLine[i] == '6' || aLine[i] == '7') aLine[i] = 'C'; if( aLine[i] == '8' || aLine[i] == '9') aLine[i] = 'D'; } } int main () { ifstream infile("lines.txt"); ofstream data("data.txt"); while(!infile.eof()) { char x; infile >> x; if( x == 'S' ) Scramble( infile, data); if( x == 'C' ) CFunc( infile, data ); } data.close(); infile.close(); return (0); }