error in input file

I have a proble with the following piece of code and I do not know what is happening, since it compiles successfully.

The purpose of the program is to allow the user to introduce the name of the file and store each line in a string container, but when I try to use it, only the "cout<<" line appears, and it will not even let me write the name of the file.

The code is:
[
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;

vector<string> take_put(ifstream & infile){
vector<string> infolines;
string filename;
cout << "What is the name of the file you want to open?
cout << "Please write its extention too." << endl;
infile.open(filename.c_str());
string line;
while(!infile.eof()){
getline(infile, line);
infolines.push_back(line);
}
return infolines;
}

int main(){
ifstream infile1;
vector<string> vector1;
vector1 = take_put(infile1);
//////////////////////////
ifstream infile2;
vector<string> vector2;
vector2 = take_put(infile2);
//////////////////////////
for(int f(0); f < vector1.size(); ++f){
cout << vector1[f] << endl;
}
for(int f(0); f < vector2.size(); ++f){
cout << vector2[f] << endl;
}


}
]

I would be very grateful if someone could tell me what is wrong.
You never actually get anything into the string.
what kind of programming language is it? i think c++?

if it is c++ language this is the correct:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;

{
vector<string> take_put(ifstream & infile){
vector<string> infolines;
string filename;
cout << "What is the name of the file you want to open?
cout << "Please write its extention too." << endl;
infile.open(filename.c_str());
string line;
while(!infile.eof())

}

getline(infile, line);
infolines.push_back(line);

{

return infolines;
}

int main()

{
ifstream infile1;
vector<string> vector1;
vector1 = take_put(infile1);
//////////////////////////
ifstream infile2;
vector<string> vector2;
vector2 = take_put(infile2);
//////////////////////////
for(int f(0); f < vector1.size(); ++f){
cout << vector1[f] << endl;

}

for(int f(0); f < vector2.size(); ++f){
cout << vector2[f] << endl;

getch()

{

thats all
I am sorry to return to this question, but my first version of the "take_put" function was intended to give me a matrix and it was written in the following way:

[
vector<vector<double> > take_put(ifstream & infile){
vector<vector<double> > matrix;
vector<string> infolines;
string filename;
cout << "What is the name of the file you want to open?";
cout << "Please write its extention too." << endl;
getline(cin,filename);
infile.open(filename.c_str());
string line;
while(!infile.eof()){
getline(infile, line);
infolines.push_back(line);
}
int information, EnergyRange;
cout << "Insert variable 1" << endl;
cin >> information;
cout << "insert variable 2" << endl;
cin >> EnergyRange;
matrix = infomatrix(infolines, information, EnergyRange);
return matrix;
}
]

As it is possible to see, I has actually putting something in the getline(cin, filename) string. But the function did not work, and as I said earlier, I thought the problem was at this spot, since the program misteriously stopped working after asking for the file's name.

After some effort I was able to write a version of this function that works:

[
vector<vector<double> > take_put(ifstream & infile, int information, int EnergyRange){
vector<vector<double> > matrix;
vector<string> infolines;
string filename;
cout << "What is the name of the file you want to open?";
cout << "Please write its extention too." << endl;
getline(cin,filename);
infile.open(filename.c_str());
string line;
while(!infile.eof()){
getline(infile, line);
infolines.push_back(line);
}
matrix = infomatrix(infolines, information, EnergyRange);
return matrix;
}
]

The difference between them is very little: in the first version, "information" and "EnergyRange" (used by the "infomatrix" function in the "take_put" function) are not given as parameters and I ask for them within the function's body.

In the second version, which works, I reshaped the "take_put" function in such a way that "information" and "EnergyRange" are given as parameters.

As I am a beginner, it took me quite a lot of time before I could realise this was the problem, and for that, I apologize to those who took the trouble to answer. But know that the problem is solved, that leaves me with one question:

Why the first function works and the second does not? In this case, because of the fact that the "infomatrix" function need the "information" and "EnergyRange" parameters why can't I simply just ask for them within the body of the "take_put" function?

Topic archived. No new replies allowed.