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:

                          i
2
3
4
v
6
7
viii
9
                          std::istream &readLinesAndDoStuff(std::istream &input) {     std::string line;                            while(std::getline(input, line))     {         doStuff(line);     }                            return                            input;                            //traditional, not required if y'all want a different return type                            }                        

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

initialize

function - 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

Scanner

course? It sounds to me like your professor is trying to bring Java with him into C++. We have no thought how the

Scanner

class is defined or how it should be used unless you lot can provide information technology.

Also, if your professor requires the use of the

initialize

part, 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?

                          1
2
3
4
five
six
7
8
9
10
11
12
13
14
15
16
17
xviii
nineteen
twenty
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
                                                      void                            EditDistance::initialize(Scanner& scanner) {                            //int colSize;                            //int rowSize;                            string line1;                            //strings for input lines                            string line2;                            int                            words1 = 0;                            //number of words in strings                            int                            words2 = 0;   cord blank =                            " xx";                            while(scanner.hasNext())   {     sentence1.push_back(getline(scanner,line1);     sentence2.push_back(getline(scanner,line2);   }                            int                            length1 = sentence1.length();                            int                            length2 = sentence2.length();                            for(int                            i = 0; i < length1; i++)                            //finds number of words in sentence1                            {                            if(sentence1[i] ==                            ' ')     {       words1++;     }   }                            return                            words1;                            for(int                            i = 0; i < length2; i++)                            //finds number of words in sentence2                            {                            if(sentence2[i] ==                            ' ')     {       words2++;     }   }                            return                            words2;                            if(words1 != words2)                            //appends blanks to sentence1 if needed                            {                            if(words1 < words2)     {                            while(words1 != words2)       {         sentence1.append(blank);       }                            return                            sentence1;     }                            if(words1 > words2)                            //appends blanks to sentence2 if needed                            {                            while(words1 != words2)       {         sentence2.suspend(bare);       }                            return                            sentence2;     }   }                            for(int                            i = 0; i < words1; i++)                            //creates 2d vector                            {     vector<int> row;     dist.push_back(row);   } }                        

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel