How Do You Use a Scanner to Read in Input From the Console?
Using a Scanner to read input from file to vectors
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 gear up to scanner. I am completely stuck on how i tin utilise 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.
void EditDistance::initialize(Scanner& scanner)
{
Scanner& scan = scanner;
string line1;
string line2;
while(browse.hasNext())
{
sentence1 = getline(browse,line1);
sentence2 = getline(scan,line2);
}
}
What is a
Scanner? C++ is not Java.
This line just creates a new name for an object which already exists:
Scanner& browse = scanner;
There's no reason to do that.
If you want to read each line from a stream, you demand to use the C++ streams library correctly:
| |
If yous want to read all the strings, you lot only demand to push button them into a vector and then return the vector (or read them into the vector in your class).
Past the way, information technology is bad do to have an
initializefunction - use a constructor instead.
Last edited on
The part is given to me for an assignment. I am dislocated then what the parameter is used for then. The goal is to create a Needleman-Wunsch program
If it'southward for an assignment and so I assume you were also given the definition of the
Scannercourse? It sounds to me like your professor is trying to bring Java with him into C++. We have no thought how the
Scannerclass is defined or how it should be used unless you lot can provide information technology.
Also, if your professor requires the use of the
initializepart, I am bewildered. That's a very C thing to do. I don't sympathize how they could mix so many other languages into C++ like this.
Concluding edited on
Also how can i specify which line goes to which vector?
They have dissimilar names, exercise they not?
They do. I guess i just don't understand how getline() works footstep by footstep. This is what im trying now:
void EditDistance::initialize(Scanner& scanner)
{
string line;
ifstream input1("xinput1.txt");
while(getline(input1,line))
{
sentence1.push_back(line);
sentence2.push_back(line);
}
}
Why are you making a file stream and ignoring the parameter passed to your part? I'chiliad pretty sure that's not what you're supposed to be doing.
Your original code was really closer - you only used
=by mistake instead of
.push_back()similar y'all intended.
Concluding edited on
This is what i have put together now. Does it look similar im going in the right direction now?
| |
Last edited on
Please edit your mail and surround your code with [code]code tags[/code] and so that is has syntax highlighting.
Why practice you have render statements?
This is one of the beginning C++ programs ive ever worked with so im still learning how information technology all works. I take been learning mostly from online sources and then im notwithstanding missing a lot of information on the basics.
Topic archived. No new replies immune.
yamamotothung1969.blogspot.com
Source: https://www.cplusplus.com/forum/beginner/142829/
0 Response to "How Do You Use a Scanner to Read in Input From the Console?"
Post a Comment