I am trying to complete this program. It is excuting the second part of it, Which every 5th word needs to be replaced with an underline. But I need to print out the text with out the underlines first. then have it replaced with underlines. Also, the user has to enter the name of the file "spartan.txt" then it will produce orginal the file, and then it would reproduce it with the underlined section. I am not asking for the answer, JUST some guidance on what may have gone wrong. Thank you.

EXAMPLE on how it should show:
"On the banks of the Red Cedar, There's a school that's known to all; Its specialty is winning, And those Spartans play good ball; Spartan teams are never beaten, All through the game they fight; Fight for the only colors: Green and White."

"On the banks of the ______ Cedar, There's a school ______ known to all; Its ________ is winning, And those ______ play good ball; Spartan ______ are never beaten, ...."

Only the underlined part is coming out.

#include <iostream> // include standard I/O library
#include <fstream> // include standard file library
#include <iomanip> // include IO manipulators
#include <string> // include C++ string class
#include <cctype> //
using namespace std; // access standard namespace

int main ()
{
ifstream Input;
string fileName;


  // variables 
char name;
char last = ' ';
int blank = 1;

// book operators
bool is5blank = false;
bool print = false;

// constants 
const string UNDERLINE = " ________";

cout << "Please enter the file name with .txt at the end" << endl;
cin >> fileName; 

cout << "\n==================" << endl; 
cout << "Cloze Version of Text" << endl; 
cout << "==================" << endl << endl; 

Input.open(fileName.c_str());

while (!Input)
{
cout << "Please re-enter the file name" << endl;
cin >> fileName; 
cout << endl; 
Input.open(fileName.c_str());
} 

Input.clear(); // reset read pointer to beginning of file
Input.seekg(0L, ios::beg); 

cout << endl << endl; 

while ((name = Input.get()) != EOF)
{
if (isalpha(name))
{
if(blank == 0)
{
if(print == false)
{
cout << UNDERLINE; 
print = true;
} 
}
else 
cout << name;
}
else
{
if(isalpha(last))
blank++;

if(blank == 5)
{
blank = 0;
print = false;
} 
cout << name;

}

last = name;
}





Input.close();

return 0;

}

You are doing it the most diffiult way possible -- reading the file one character at a time. Words are separated in files with white space (spaces and/or tabs). ifstream has >> operator which reads entire words

std::string word;
ifstream input(filename.c_str());
int counter = 0;
// check that file was opened
if( input.is_open())
{
    while( input >> word ) // read an entire word at one time
    {
       ++counter;
       if( counter % 5 == 0)
       {
          // output underline
       }
       else
       {
          // output original word
       }
     }
}

Edited 6 Years Ago by Ancient Dragon: n/a

So what your saying is This part of my code:

std::string word;
ifstream input(filename.c_str());
int counter = 0;
// check that file was opened
if( input.is_open())
{
    while( input >> word ) // read an entire word at one time
    {
       ++counter;
       if( counter % 5 == 0)
       {
          // output underline
       }
       else
       {
          // output original word
       }
     }
}

Could be re-done by using

std::string word;
ifstream input(filename.c_str());
int counter = 0;
// check that file was opened
if( input.is_open())
{
    while( input >> word ) // read an entire word at one time
    {
       ++counter;
       if( counter % 5 == 0)
       {
          // output underline
       }
       else
       {
          // output original word
       }
     }
}

Edited 6 Years Ago by Spartan_MSU12: n/a

I just re-read the criteria, and I was in the right direction. Even though using the isalpha is the longer way to go. He wants us to count characters instead of whole words. So I am stuck at the same point agian.

Edited 6 Years Ago by Spartan_MSU12: n/a

>>Which every 5th word needs to be replaced with an underline

You mean that isn't right? Then what is it? The only way to do that is to count words. I suppose you could read the file one character at a time then check for isspace() to find the end of words. Something like this -- which was not compiled nor tested, so it might contain errors.

int counter = 0;
char c;
ifstream infile("filename.txt");
while( infile.get(c) )
{
   if( isapce(c) )
   {
      ++counter;
      // now ignore all consecutive spaces
      int ch = infile.peek();
      while( ch != EOF && isspace(ch) )
      {
         infile.get();
         ch = infile.peek();
      } 
   }
}
This article has been dead for over six months. Start a new discussion instead.