Pass Line As Parameter

I'm having problems on how to pass a line from a stream/getline as a function parameter.

I have a text file that is multiple lines long and my program needs to read the first line of it & send it through the function as a parameter and repeat until all of the file's lines have been read.

I've been trying to mess with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
main()
ifstream in_file2(DNA_strings);
istringstream iss2(line)
while (getline(in_file, line))
{
 string x = decode(map, in_file)
}


Function:
string decode(map<string, string>&map, istringstream &in_file)
{
string line;
cout << line << endl;
}



The error I'm getting says there's an undefined reference in my function. Am I even close to what I'm trying to get done anyway?
Last edited on
getline(in_file, line)

Once you do this.... the string data is put into your line object.


string decode(map<string, string>&map, istringstream &in_file)

You do not need to pass the input file to decode. You are not decoding the entire file... you are just decoding one line.

Therefore the function should take a string instead of a istringstream as the 2nd parameter. The string you want to give it will be a string containing the line.

Then... main can call decode with the line object that it pulled out of the file.
Thanks Disch, I got it to at least compile now! :) However, now I'm being a derp somewhere else.

Changed main to:
1
2
3
4
while(getline(in_file, line))
{
string x = decode(map, line);
}


And changed the function to:
1
2
3
4
5
6
string decode(map<string, string>&map, string &line)
{
cout << line << endl;  //Testing if the lines are actually being sent through
cout << &line << endl;
return 0;
}


I'm not getting any output for either of the 2 cout statements, what am I screwing up?
Last edited on
Don't see anything wrong with that. Are you sure the file was opened successfully?

Check in_file.good() before calling getline to see if there was a problem with opening the file.
Last edited on
Found it, I had a file earlier and have them named in_file and in_file2, forgot the 2. Thanks for all the help! :)
Last edited on
Topic archived. No new replies allowed.