Still stuck on white spaces

My assignment asks me to read in a .txt file, change all text to uppercase and then delete all the extra white spaces. Code so far:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>

using namespace std;

int main()
{
ifstream in_stream;

in_stream.open("data10.txt");

if (in_stream.fail())
{
cout << "Input file, data10.txt, failed to open.\n";
exit(1);
}

do

{
char symbol;
in_stream.get (symbol);
symbol = toupper(symbol);
cout << symbol;
}

while (in_stream);

in_stream.close();

return 0;
}

Now to rid the extra white spaces, I was told to use a loop and not isspace etc. Just a simple if statement, if (symbol == ' ') then delete the whitespace. But my problem is, if I do this:

if (symbol == ' ')
cout << '\b';

that will delete all white spaces and scrunch all the text together. What can I add in the do/while loop above that will only delete the "extra" white space, but leave a space between each word?

Some of you attempted to answer my question, but I still can't seem to make this work.
You can do this with keeping record of whether the last character was a space
So I can do something like this:

if (previous symbol is ' ' and all upcoming symbols are spaces)
cout << delete all upcoming spaces.

How can I code for "previous symbol"?
Since it is a loop, you can make an iteration modify the behaviour of the next one.

You can set a bool to true when you printed a whitespace, unset it when you don't have a whitespace and print the current character only when that bool is false.
Or
Declare a char and set it to the current character only after having checked it against the current character - so it will hold the old value -
Last edited on
Topic archived. No new replies allowed.