Hello all,

I am working on a program that takes input from a file, removes extra white spaces and outputs the edited text to another file.

So far my program deletes 1 whitespace whether the whitespace should be deleted or not.

I have read about peek() and putback(). I am pretty sure I need to implement these two functions but quite frankly I am not sure how to go about doing this.

I don't want anyone to just give me the answer I would really appreciate it if someone gave me a hint as to where my logic went south or my structure or enlighten me about some predefined function you think I may not be aware of.

Any help is appreciated. Thank you.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;
void check_blank(ifstream& instream, ofstream& outstream);
int number = 0; //keeps track of how many spaces have been deleted

int main() 
  ifstream instream;
  ofstream outstream;
  bool     t;
  instream.open("hw4pr6input.txt");         //Open file: text to be edited
  if(!instream.fail())                      //Make sure file opened successfully
  cout << "test from open";
     //Call function that finds blanks and edits
    cout << "File failed to open (1st)";
  outstream.open("hw4pr6output.txt");       //Open file: edited text will go
  if(!outstream.fail())                     //Check if open failed
   t = true;   
   check_blank(instream, outstream);              //Call function to input edited text into file
    cout << "File failed to open (2nd)";
  cout << "Removed " << number              
       << " blanks from file" 
       << endl;
  instream.close();                         //Close both files
  return 0;
}                                                  //End main

void check_blank(ifstream& instream, ofstream& outstream)
  char a;
  int count  = 0;                               //Variable tells if there are multiple blank spaces

  while(! instream.eof())                  //While the file isn't at the end
     if(a == ' ')                                 //If the input is a space
                                                     //Add 1 to number & count
          if(a == ' ' && count >= 2)      //If there are 2 or more spaces then do this
             outstream << ' ';
             count = 0;                         //reset count
          }  //end inner if
     }   //end outer if    
       outstream << a;  
   }//end while
}//end check function

This is my input

This is my current output

Great going with code tags :)
This    course introduces students    to the discipline of   
  computer   science, using the C++ programming   language 
as  its tool. Covered topics   will include algorithm development, 
 data  representation, logical   expressions,   sub-procedures 
and functions,    and input/output   operations.

The  course   will introduce    both object-oriented and   structured 
programming   concepts and will    emphasize good,  professional 
programming  practices.  The  course meets the basic   standards 
of   the CS1 course as defined by   the Association of Computing 
Machinery (ACM).  It is   consistent with the   first computer 
   science course in a four-year program  and should   transfer 
to   most such   programs.
This  courseintroduces students  tothe disciplineof  
 computer science, usingthe C++programming  language
as its tool.Covered topics will includealgorithm development,
 data representation,logical  expressions, sub-procedures 
andfunctions,  and input/output operations.

The course  willintroduce  both object-orientedand  structured
programming  conceptsand will  emphasizegood, professional 
programming practices. The coursemeets thebasic  standards
of  theCS1 courseas definedby  theAssociation ofComputing 
Machinery(ACM). It is consistent withthe  firstcomputer 
 science coursein afour-year program andshould  transfer
to  mostsuch  programs.

why don't you do this the easy way by using the ifstream >> operator to remove all white space between words with output the word just read with one space

string word;
while( infile >> word)
   outfile << word << " ";
plain and simple

why don't you do this the easy way by using the ifstream >> operator to remove all white space between words with output the word just read with one space

string word;
while( infile >> word)
   outfile << word << " ";

Like Larry Wall said:

"Make simple things easy."

>But what about the newlines and/or tabs?
Indeed. And what about such monstrosities as "\t \n\n\t"? Does "extra whitespace" refer to adjacent whitespace characters of the same value, any value that isspace returns true for, or are we only working with the ' ' character? The problem doesn't seem to be well defined.

>I have read about peek() and putback().
Unnecessary. You can save the last character read from the stream and use it as a state to process the next character:

char last = 0;
char ch;

while ( in.get ( ch ) ) {
  if ( ch == ' ' && last == ' ' )

  out.put ( ch );
  last = ch;

A good rule of thumb is that if you think you need to peek or putback on a stream, you probably need to improve your design.

tabs are considered white space so they are treated just like spaces. But you (jamthwee) are correct about newlines, so my previous suggestion will not work. But there is hope. Use getline() to read the entire line then stringstream to split it up into individual words similar to what I posted before. I was about to post the solution, but then I would be doing the OPs homework for him wouldn't I ?

Thank you for all the pointers everyone. I really appreciate it.

This question has already been answered. Start a new discussion instead.