Hello,
I have been lurking on this site for a while, reading other people's programming questions and answers in the hopes that I'll learn something. I'm in my first programming class right now, and I think I'm confused about nested loops and how to keep a program looping until it's completed.
So the problem I'm having right now is that I'm supposed to write a program that removes extra spaces from an incoming data file, and then puts the corrected sentence or paragraph back into another file. I can get it to delete one space each time it encounters a space, or I can get it to replace every space with a dash, or I can get it to add a space. But I can't seem to get it to remove only extra spaces and leave single spaces where they belong.
I'm pretty sure I'm not doing the loops right, but I'm sure there are other problems as well. I've been working on this one simple problem for several days, and would really, really appreciate any help you could give!!
I'm only going to include the function definition since that's where I actually tell the program what to do with the file... (this program removes one space, and sometimes it also removes a letter which it's not supposed to do.)
void remove_extra_spaces(ifstream& in_stream, ofstream& out_stream)
{
char next, previous;
int counter = 0;
in_stream.get(next);
cout << next;
while (! in_stream.eof())
{
in_stream.get(next);
cout << next;
out_stream << next;
if (next == ' ')
{
previous = next;
in_stream.get(next);
if (next == ' ' && previous == ' ')
{
cout << "";
out_stream << "";
counter++;
}
else
{
in_stream.get(next);
cout << next;
out_stream << next;
}
}
else
{
in_stream.get(next);
cout << next;
out_stream << next;
}
}
cout << endl << endl;
cout << "There are " << counter << " characters in this document.\n";
cout << endl << endl;
}