stack122adam
stack122adam

Reputation: 13

Is there a reason that not the whole string is printed?

I'm writing a program that reads each line from a text file and stores each word in a vector. I noticed when running the program that not the whole initial text is printed. Any idea why?

This is the file:

It was a Thursday, but it felt like a Monday to John. And John loved Mondays. He thrived at work. He dismissed the old cliché of dreading Monday mornings and refused to engage in water-cooler complaints about “the grind” and empty conversations that included the familiar parry “How was your weekend?” “Too short!”. Yes, John liked his work and was unashamed.

I should probably get another latte. I’ve just been sitting here with this empty cup. But then I’ll start to get jittery. I’ll get a decaf. No, that’s stupid, it feels stupid to pay for a decaf. I can’t justify that.

John was always impatient on the weekends; he missed the formal structure of the business week. When he was younger he used to stay late after school on Fridays and come in early on Mondays, a pattern his mother referred to with equal parts admiration and disdain as “studying overtime.”

Jesus, I’ve written another loser.

Now, John spent his weekends doing yard work at the Tudor house Rebecca left him after their divorce. Rebecca, with her almond eyes—both in shape and in color—could never be his enemy.

That barista keeps looking at me. She’ll probably ask me to leave if I don’t buy something. She’s kind of attractive. Not her hair—her hair seems stringy—but her face is nice. I should really buy something.

Their divorce was remarkably amicable. In fact, John would often tell his parents, “Rebecca and I are better friends now than when we were married!” In fact, John looked forward to the days when he and Rebecca, with their new partners, would reminisce about their marriage, seeing it in a positive light, like two mature adults.

Maybe I’ll just get a pumpkin-spice loaf. That way I can still sit here without going through a whole production of buying a coffee and giving my name and feeling like an asshole while it gets made.

And the code is:

#include <iostream>
#include <fstream>
#include <vector>


std::vector<std::string> split(const std::string& s, char delim = ' ');

int main()
{   
    std::cout << "File name: ";
    std::string fname;
    std::cin >> fname;

    std::ifstream inpob {fname, std::ios_base::in};

    if (!inpob.is_open()) std::cout << "Error opening file " << fname;
    else
    {
        std::string str = "";
    
        for (std::string line; std::getline(inpob, line);)
            str += line;
    
    
        std::vector<std::string> splited = split(str);
    
        for (std::string word : splited) std::cout << word;
    
        std::cout << "\nWords: " << splited.size() << "\n";
    }

    return 0;
}


std::vector<std::string> split(const std::string& str, char delim)
{
    std::vector<std::string> v;

    std::string word = "";

    for (char c : str)
    {
        if (c == delim)
        {
            word += delim;
            v.push_back(word);
            word = "";
            continue;
        }
        word += c;
    }

    return v;
}

Upvotes: 0

Views: 78

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You forgot to add the final word to the vector.

std::vector<std::string> split(const std::string& str, char delim)
{
    std::vector<std::string> v;

    std::string word = "";

    for (char c : str)
    {
        if (c == delim)
        {
            word += delim;
            v.push_back(word);
            word = "";
            continue;
        }
        word += c;
    }

    if (word != "") v.push_back(word); // add this
    return v;
}

Upvotes: 1

Related Questions