Hi, I'm in a beginning class in C++ and we need to make an assignment that inputs a file, prints it to the screen, and then prints it to the screen again with an underscore every 5 blanks. I got the last part to work, but I can't get it to print to the screen twice once without the underscores and once with the underscores.

It is also a requirement to use this in my program, and I'm not sure where it will go:

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

Any help would be greatly appreciated.

#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 << "ECHOPRINTED 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 very 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;

}

Recommended Answers

All 2 Replies

Do you need it to be printed with the same loop? or try to use another while loop without any conditions? Changes in bold.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
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 << "ECHOPRINTED 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());
}

[B]while(!Input.eof())
{
    string Newline;
    getline(Input, Newline);
    cout << Newline << "\n";
}
[/B]
Input.clear(); // reset read pointer to very 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();

getchar();
getchar();
return 0;
}

thank you! That was very helpful! Another loop is allowed. However, my professor requires the information not to be read as a string, but as the individual characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.