-
Skip spaces project
I am doing a C++ program for school which is supposed to take letters from an input file and output them in a file. the input file has words with various number of spaces between them. The program is supposed to output the words with just one space between them, like a normal sentence. I have it all written up, the main codes are
infile.get(c)
while (! infile.eof())
( if (c != ' ')
outfile.put(c);
else
outfile.put(c);
infile.get(c);
skip(infile, outfile, c);
void skip(ifstream& infile, ofstream& outfile, char& c)
{
while (c == ' ')
{
infile.get(c)
}
every time I run it, it just ends up skipping all the spaces. If anyone can help me out, I'd really appreciate it, thanks!
-
find a way to only skip or add spaces where you know a gramatically correctly used space would occur... [i find that quite a task...] give us any example of the correct output and your input... plus, i would doubt that your instructor would give such an assignment... maybe i have the wrong idea about it's task...
-
There are a few syntax errors in your program please post the entire program and I will show you where your problems are.
-
program
this is my whole program thus far
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
void skip(ifstream& infile, ofstream& outfile, char& c);
int main ()
{
ifstream infile;
ofstream outfile;
infile.open("start.txt");
if (infile.fail())
{
cout << " Input file opening failed.\n";
exit (1);
}
outfile.open("end.txt");
if (outfile.fail())
{
cout << " Output file opening failed.\n";
exit (1);
}
char c;
infile.get(c);
while (! infile.eof())
{
if (c != ' ')
outfile.put(c);
else
outfile.put(c);
infile.get(c);
skip(infile, outfile, c);
infile.get(c);
}
infile.close();
outfile.close();
return 0;
}
void skip(ifstream& infile, ofstream& outfile, char& c)
{
while (c == ' ')
{
infile.get(c);
}
infile.putback(c);
}
It's supposed to take an input like
Hi there this is the file ( more than one space between words)
and output it so it reads
Hi there this is the file
but I can only get
Hitherethisisthefile
I've been playing around with it tonight and no matter what I change it either shows all the letters but no spaces or shows the spaces but knocks off the first letter of each word. If you have any ideas please let me know, thanks
-
ooh! i see now... run through your file like this...
if a space is in the current character, check if a space is in the next character, delete if it is, else do not delete... if it is not a space, go to the next character...
-
Looks like its working now. Please note the changes I've made. If your serious about programming send me an email explaining what I've change. You basically had it.
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
void skip(ifstream& infile, ofstream& outfile, char& c);
int main ()
{
ifstream infile;
ofstream outfile;
infile.open("c:\\start.txt");
if (infile.fail())
{
cout << " Input file opening failed.\n";
exit (1);
}
outfile.open("c:\\end.txt");
if (outfile.fail())
{
cout << " Output file opening failed.\n";
exit (1);
}
char c;
infile.get(c);
while (! infile.eof())
{
if (c != ' ')
{
outfile.put(c);
}
else
{
outfile.put(c);
infile.get(c);
skip(infile, outfile, c);
}
infile.get(c);
}
infile.close();
outfile.close();
return 0;
}
void skip(ifstream& infile, ofstream& outfile, char& c)
{
while (c == ' ' && ! infile.eof( ) )
{
infile.get(c);
}
if( !infile.eof( ) )
{
outfile.put(c);
}
}