0

I have 2 methods of a class HashTagger.

The first (void getTags(std::string line);) takes an input and produces a string formatted as I would like.

The other (void printTags();) should take that string and print it to console with a simple enough method.

However, the output never comes am I missing anything silly.

here is the main

#include "HashTagger.h"
#include <string>
#include <iostream>
using namespace hw02;
using namespace std;


int main() {

  // Construct an object for extracting the hashtags.

  HashTagger hashTagger;

  // Read the standard input and extract the hashtags.
  while (true) {

    // Read one line from the standard input.
    string line;
    getline(cin, line);
    cout << line << endl;
    if (!cin) {
      break;
    }

    // Get all of the hashtags on the line.
    hashTagger.getTags(line);
  }

  // Print the hashtags.
  hashTagger.printTags();

  // Return the status.
  return 0;
}

Update with source methods

#include "HashTagger.h"
#include <iostream>
using namespace std;
using namespace hw02;

void HashTagger::getTags(string line) {

    int h = 0;
    int i = 0;

    //add a space after line, so we can check for end of line
    line = line + " ";

    // Loop over all characters in a line that can begin a hashtag
    for (unsigned int j = 0; j < line.length(); ++j) {
        char c = line.at(j);

        // if "#" is found assign beginning of a hashtag to h
        if (c == '#') {
            h = j; //h is the beginning char of the hashtag
            i = 1; //signifies that a hashtag has been begun

        // checks that a hashtag has begun, then looks for a newline, ".", "?", or "!" and adds substring of the hashtag to hashtags_
        } else if (i == 1 && (c == ' ' || c == '\r' || c == '.' || c == '?' || c == '!' )) {
            hashtags_ = hashtags_ + "\n" + line.substr(h, j - h);
            h = 0;
            i = 0;
        }
    }
    //TEST// cout << hashtags_ << endl;
}

After the last cycle through main loop, this is producing the output that I want as shown by the test in the last line above. However I would like that variable to carry into the printTags() method as output using the same cout << call.

void HashTagger::printTags() {

    // print out hashtags_ to the console
    cout << hashtags_ << endl;
}

and lastly header

#ifndef HASHTAGGER_H
#define HASHTAGGER_H

#include <string>

namespace hw02 {

class HashTagger {
public:
    void getTags(std::string line);

    void printTags();

    std::string hashtags_;

};

}

#endif

Here is the test input

Test#of hash#tagging
#even!#when #starting? a line
or #containing a #comma,

and expected output

#of
#tagging
#even
#when
#starting
#containing
#comma,
Vyff
  • 83
  • 6
  • You need to include `getTags` to show how the string is being constructed. – Mikel F Jan 26 '17 at 00:26
  • 3
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Dan Korn Jan 26 '17 at 00:26
  • Hard to say without full code but, pretty sure your printTags is missing a `this` – cadolphs Jan 26 '17 at 00:29
  • @Lagerbaer No, `this` is not required to resolve member variables from a member function. – Fantastic Mr Fox Jan 26 '17 at 00:36
  • Do you have a main...? The other possible option is that it is in fact printing it out but since the string was not initialized the output is nothing. You may also want to consider using regular expressions to solve this instead of trying to roll your own parser. – MS-DDOS Jan 26 '17 at 00:36
  • updated with all parts of the program, I don't know the balance between minimal and complete, but given that it is a fairly small program, I gave complete – Vyff Jan 26 '17 at 00:38
  • How are you getting `cin` to fail to break out of your input loop? Are you piping a file to stdin? – zdan Jan 26 '17 at 00:58
  • Yes I am piping a file. I added the test input to the bottom. – Vyff Jan 26 '17 at 01:02
  • What's the output supposed to look like? What I get looks sane. Mind you, I replaced `cin` with a `stringstream`. That might be the difference. – user4581301 Jan 26 '17 at 01:05
  • added expected output as well. – Vyff Jan 26 '17 at 01:12

1 Answers1

0

I figured out why output was not coming out when I pressed return. Because the while(true) loop break had to hit an end of file signifier of some sort and had very strict rules in my IDE, it would only end the loop if I hit CTRL + D for EOF on my mac. But not just that, I had to enter the console window, exit it, and then enter it again due to some bug.

found the answer at Passing End of Transmission (Ctrl + D) character in Eclipse CDT console

Community
  • 1
  • 1
Vyff
  • 83
  • 6