I have an input file that is made up of two lines with one sentence on each. My vectors: sentence1, sentence2
The input file contents:
this is the first line
this is the second and final line
The parameter of the function is Scanner& scanner and i have created Scanner& scan to be set to scanner. I am completely stuck on how i can use this to read the lines in the file and assign them to the vectors. I am trying to use something along the lines of this code but im not sure if im going in the right direction.
This line just creates a new name for an object which already exists:
Scanner& scan = scanner;
There's no reason to do that.
If you want to read each line from a stream, you need to use the C++ streams library correctly:
1 2 3 4 5 6 7 8 9
std::istream &readLinesAndDoStuff(std::istream &input)
{
std::string line;
while(std::getline(input, line))
{
doStuff(line);
}
return input; //traditional, not required if you want a different return type
}
If you want to read all the strings, you just need to push them into a vector and then return the vector (or read them into the vector in your class).
By the way, it is bad practice to have an initialize function - use a constructor instead.
The function is given to me for an assignment. I am confused then what the parameter is used for then. The goal is to create a Needleman-Wunsch program
If it's for an assignment then I assume you were also given the definition of the Scanner class? It sounds to me like your professor is trying to bring Java with him into C++. We have no idea how the Scanner class is defined or how it should be used unless you can provide it.
Also, if your professor requires the use of the initialize function, I am bewildered. That's a very C thing to do. I don't understand how they could mix so many other languages into C++ like this.
This is one of the first C++ programs ive ever worked with so im still learning how it all works. I have been learning mostly from online sources so im still missing a lot of information on the basics.