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

void EditDistance::initialize(Scanner& scanner)
{
Scanner& scan = scanner;
string line1;
string line2;

while(scan.hasNext())
{
sentence1 = getline(scan,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& 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.
Last edited on
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.
Last edited on
Also how can i specify which line goes to which vector?
They have different names, do they not?
They do. I guess i just don't understand how getline() works step by step. 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 function? I'm pretty sure that's not what you're supposed to be doing.

Your original code was actually closer - you just used = by mistake instead of .push_back() like you intended.
Last edited on
This is what i have put together now. Does it look like im going in the right direction now?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
  string 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.append(blank);
      }

      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 post and surround your code with [code]code tags[/code] so that is has syntax highlighting.

Why do you have return statements?
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.
Topic archived. No new replies allowed.