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,