Consecutive Blank Space Removal

I am trying to write program that will open a file, read it and create a copy with the consecutive blank spaces removed. and also keep count of the blanks and display the number that was removed from the original file.

I am not sure what i did wrong I can't even get my file to open let alone count and remove blanks. I know I need a function and a call to the function but everything i tried didn't work Any help would be super awesome thanx

Here are the contents of the file...


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.

here is my code so far...

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iomanip>
using namespace std;

int main( )
{
ifstream inputFileStream;
ofstream outputFileStream;
int blankspaces;


inputFileStream.open("hw4pr6input.txt");
if (inputFileStream.fail())
{
cout << "input file could not be opened" << endl;
}
outputFileStream.open("hw4pr6output.txt");
if (outputFileStream.fail());
{
cout << "output file could not be opened" << endl;
}

cout << "Number of blanks removed " << blankspaces <<endl;

inputFileStream.close();
outputFileStream.close();

system("Pause");
return 0;
}

closed account (2b5z8vqX)
I am not sure what i did wrong I can't even get my file to open let alone count and remove blanks.
1
2
3
4
if (outputFileStream.fail());
{
cout << "output file could not be opened" << endl;
}

A null statement follows the condition of the second if statement in main's compound statement. Anything that is within the following compound statement will be executed whether the result of the condition was false or true. Is that why you think you can't open a file?

Here's the steps that you could take to complete your assignment:
- Extract data from file stream and store in container object.
- Search for consecutive spaces in the container object.
- If found, increment counter by one and remove spaces.
- Write data stored in container object to file stream.
- Write counter to the standard output stream.

http://en.cppreference.com/w/cpp/algorithm
http://en.cppreference.com/w/cpp/io/basic_ofstream
http://en.cppreference.com/w/cpp/io/basic_ifstream
Last edited on
Ya if i run the above code i get the input file could not be opened. But it is actually opening the file and displaying the code for not opening the file?


closed account (2b5z8vqX)
Ya if i run the above code i get the input file could not be opened. But it is actually opening the file and displaying the code for not opening the file?

Does the file that the program is attempting to open exist within the directory of the executable?
i dont know my text book never said anything like that. All it said to was declare ifstreaf and ofstream and to put the file name inside (" ") marks. Is there more to it?
closed account (2b5z8vqX)
i dont know my text book never said anything like that. All it said to was declare ifstreaf and ofstream and to put the file name inside (" ") marks. Is there more to it?

Yes. Merely defining two objects of those types will not complete your assignment.
I know i need a function as well. One that can tell if blankspace > 1, then set blankspace = 1, and keep a count of the blanks removed.

... I know this is kind of offtopic, but you included iomanip twice.
Getting closer now i just need to figure out how to keep track of the blanks removed and display the number at the end.



#include <iostream>
#include <fstream>

using namespace std;
void removeblanks(istream&, ostream&);
int main( )
{
ifstream inputFileStream;
ofstream outputFileStream;
int blankspaces;


inputFileStream.open("hw4pr6input.txt");
if (inputFileStream.fail())
{
cout << "input file could not be opened" << endl;
system("pause");
exit(1);
}

outputFileStream.open("hw4pr6output.txt");
if (outputFileStream.fail())
{
cout << "output file could not be opened" << endl;
system("pause");
exit(2);
}

removeblanks(inputFileStream, outputFileStream);

cout << "Number of blanks removed " << blankspaces <<endl;

inputFileStream.close();
outputFileStream.close();

system("Pause");
return 0;
}

void removeblanks (istream& ins, ostream& outs)
{
char nextSymbol, prevSymbol = '\n';

ins.get(nextSymbol);
while (!ins.eof())
{
if ((nextSymbol != ' ') || (prevSymbol != ' '))
{
outs.put(nextSymbol);
}
prevSymbol = nextSymbol;
ins.get(nextSymbol);
}
}
Topic archived. No new replies allowed.