-4

I'm reading through the following text file word for word a replacing the words "@name@" and "@festival@". My program works perfectly for @name@ but only changes the first @festival@, but not the second. I have no idea why.

John Doe

Room 213-A

Generic Old Building

School of Information Technology

Programming State University

New York NY 12345-0987

USA

To: @name@

Subject: Season's greetings: @festival@

Dear @name@,

A very @festival@ to you and your family!

Your sincerely,

John

void Main::readFile()
{
while (v.size() != 0) {
    string name;
    name = v.at(v.size()-1);
    v.pop_back();
    std::ofstream out(name + ".txt");
    ifstream file;
    file.open("letter.txt");
    string word;
    string comma;
    char x;
    word.clear();

    while (!file.eof())
    {
        x = file.get();

        while (x != ' ' && x != std::ifstream::traits_type::eof())
        {
            if (word == "@name@") {
                word = name;
            }
            if (word == "@festival@") {
                word = "THISISATEST!!!!!!!!!!!!!!";
            }
            word = word + x;
            x = file.get();
        }

        out << word + " ";
        word.clear();
    }
}
thefreeman
  • 21
  • 1
  • 5

2 Answers2

1

The Problem is inner while condition, that it will not be true if after @festival@ exist ' '. The below code is correct

void Main::readFile()
{
while (v.size() != 0) {
    string name;
    name = v.at(v.size()-1);
    v.pop_back();
    std::ofstream out(name + ".txt");
    ifstream file;
    file.open("letter.txt");
    string word;
    string comma;
    char x;
    word.clear();

    while (!file.eof())

{
    x = file.get();

    while (x != ' ' && x != std::ifstream::traits_type::eof())
    {
        if (word == "@name@") {
            word = name;
        }
        if (word == "@festival@") {
            word = "THISISATEST!!!!!!!!!!!!!!";
        }
        word = word + x;
        x = file.get();
    }
    if (word == "@name@") {
          word = name;
    }
    if (word == "@festival@") {
        word = "THISISATEST!!!!!!!!!!!!!!";
    }

    out << word + " ";
    word.clear();
    }
}
erfan zangeneh
  • 314
  • 1
  • 2
  • 9
  • I see what you're saying, and I tried making your changes and it worked. Unfortunately, I need it to work for x amount of strings, however, at least I know what is wrong now – thefreeman Apr 28 '16 at 23:06
0

First of all, see Why is iostream::eof inside a loop condition considered wrong?

This isn't an elegant solution... but a better improvement to your original code. (I'll leave you to think of a more efficient solution):

void Main::readFile()
{
while (v.size() != 0) {
    string name;
    name = v.at(v.size()-1);
    v.pop_back();
    std::ofstream out(name + ".txt");
    ifstream file;
    file.open("letter.txt");

    string festival = "THISISATEST!!!";
    string line, nameHolder = "@name@", festivalHolder = "@festival@";
    while (std::getline(file, line))
    {
        std::size_t n_i = line.find(nameHolder);
        if(n_i != std::string::npos)
            line.replace(n_i, nameHolder.size(), name);

        std::size_t f_i = line.find(festivalHolder);
        if(f_i != std::string::npos)
            line.replace(f_i, festivalHolder.size(), festival);

        out << line << '\n';
    }
}
Community
  • 1
  • 1
WhiZTiM
  • 19,970
  • 3
  • 36
  • 56