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;
}

...sorry, I forgot to update what the counter thing said... I'm trying to count the number of spaces deleted, NOT the number of characters in the stream.

Code tags usually make it much easier to read, but in your case, there is no formatting either so it doesn't help much. Format your code and repost it in code tags please.

[code=cplusplus] // paste code here

[/code]

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;
}

Lines 19 and 20 don't make much sense to me. What's the point of outputting an empty string? Those lines can be deleted. The code is impossible to follow without the proper indentation. You want something like this:

if (a == b)
{
    if (c == d)
    {
        cout << "Hello world.";
        // more code
    }
    else
    {
        cout << "Hi world."
        // more code
    }
}
else
{
    cout << "Goodbye world.";
    // more code
}

Styles vary, but I prefer each new nested if-statement block to be indented by about four spaces (spaces, not tabs. Tabs look terrible when posted on Daniweb).

My original code had all the indenting, but when I copied and pasted it did what you see.

I actually managed to figure out the problem. My problem mostly was that I wasn't troubleshooting by following each letter through the loops to see what would happen.

Next time, if I have a problem I'm banging my head against the wall over, I will make sure to put all the indents in so it's easier to read.

Thanks for being willing to help me...

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.