Hello. What i'm trying to do is simple and I know that I could just get the answer offline. However, I wanted to finish this one myself, so if someone could fix what I believe to be a small error I would appreciate it.

I am simply trying to read a string from an input file and remove spaces such that there is only one space in between words, where in the input file there can be multiple spaces in between words. As of now I open the file and it removes all of the spaces. I will explain my algorithm in a second. Here is what I have:

The input file simply contains text, which reads:

This is the input file for Programming Project 6 on p. 366.

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

using namespace std;
int main()
{
	ifstream in_stream;

	in_stream.open("proj-8.dat");

	char character;
	character=false;

	do{
		in_stream.get(character);

	if(character != ' '){
		cout << character;

		character=false;
	}

	else if(character==false)
	{
		cout << character;
		character==true;
		in_stream.get(character);
	}
	} while (! in_stream.eof());
	in_stream.close();
}

As it stands what i'm trying to do is to set the variable character to false. If the first character is a letter (i.e. not a space), then it will print that character and then reset character to false. If it is a space it will print it and be set to true, so that the next time through the loop if it is another space neither condition will be satisfied and nothing will happen. Like I said, at this point it is just removing all of the spaces. It's also giving me a square at the end after my period...that I dont get.

this is currently my output:

ThisistheinputfileforProgrammingProject6onp.366. <--then there's a square right here.


Thank you in advance for the help! People here have been very friendly so far.

When you hit a space, write it out
then find all sebsequent spaces and throw them away.

commented: You're kidding. Really? How'd you come up with that? :icon_rolleyes: -3

Hello. What i'm trying to do is simple and I know that I could just get the answer offline. However, I wanted to finish this one myself, so if someone could fix what I believe to be a small error I would appreciate it.

I am simply trying to read a string from an input file and remove spaces such that there is only one space in between words, where in the input file there can be multiple spaces in between words. As of now I open the file and it removes all of the spaces. I will explain my algorithm in a second. Here is what I have:

The input file simply contains text, which reads:

This is the input file for Programming Project 6 on p. 366.

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

using namespace std;
int main()
{
	ifstream in_stream;

	in_stream.open("proj-8.dat");

	char character;
	character=false;

	do{
		in_stream.get(character);

	if(character != ' '){
		cout << character;

		character=false;
	}

	else if(character==false)
	{
		cout << character;
		character==true;
		in_stream.get(character);
	}
	} while (! in_stream.eof());
	in_stream.close();
}

As it stands what i'm trying to do is to set the variable character to false. If the first character is a letter (i.e. not a space), then it will print that character and then reset character to false. If it is a space it will print it and be set to true, so that the next time through the loop if it is another space neither condition will be satisfied and nothing will happen. Like I said, at this point it is just removing all of the spaces. It's also giving me a square at the end after my period...that I dont get.

this is currently my output:

ThisistheinputfileforProgrammingProject6onp.366. <--then there's a square right here.


Thank you in advance for the help! People here have been very friendly so far.

Use peek to see what's ahead one character. This is probably the easiest way, but maybe not the most efficient.

while (in_stream.peek() == ' ') in_stream.get(); //get rid of white spaces at beginning.

while (in_stream.good()){
  if (in_stream.peek() == ' ') {cout << in_stream.get();} //cout 1 space and advance 1.
  while (in_stream.peek() == ' ') {in_stream.get();}  //get rid of rest of spaces
  cout << in_stream.get();  //cout the non space characters
}

-Greywolf

Use peek to see what's ahead one character. This is probably the easiest way, but maybe not the most efficient.

Yes, inefficient, and bad. You have to read the next character anyway, so why peek, then read?

Look at line 27. Is that an assignment?

Yes, inefficient, and bad. You have to read the next character anyway, so why peek, then read?

Look at line 27. Is that an assignment?

lol you're right... I do some crazy things when I first wake up. Here's without the peeks.

char A;
while (in_stream.good()){
  while (in_stream.get() == ' ') {}  //get rid of white spaces
  while (in_stream.get(A) != ' ' && in_stream.good()) {cout << A;}  //cout all non spaces
  cout << " ";  //manually enter one space;
}

I meant:
while (in_stream.good() && in_stream.get(A) != ' ')
not the other way around. This is why I never got good grades in school :)

I meant:
while (in_stream.good() && in_stream.get(A) != ' ')
not the other way around. This is why I never got good grades in school :)

Well, since we frown upon giving solutions to people, maybe you should refrain from posting code anyway. After all you don't get the grades. They do...

Well, since we frown upon giving solutions to people, maybe you should refrain from posting code anyway. After all you don't get the grades. They do...

oops :cool:

I meant:
while (in_stream.good() && in_stream.get(A) != ' ')
not the other way around. This is why I never got good grades in school :)

I'm not so good at this so far. But, wouldn't this input a single space between each character? It seems to me that it would remove all spaces but then every time it found a non-space it would then input a space.

Look at line 27. Is that an assignment?

You're right. So I changed it to:

character=true;

But it still doesn't work. I'm not sure why this won't work still. It seems like the algorithm should work the way I want it to.

So post what you have now.

So post what you have now.

Sure, it's not so different than what I had before. I just changed the one assignment in line 27. So as I see it it's not entering the second if statement at all to print out the spaces. That is, I think, what I need to change. I'm not confident that greywolfs method would work, so i'm sticking with what I have.

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

using namespace std;
int main()
{
	ifstream in_stream;

	in_stream.open("proj-8.dat");

	char character;
	character=false;

	do{
		in_stream.get(character);

	if(character != ' '){
		cout << character;

		character=false;
	}

	else if(character == false)
	{
		cout << ' ';
		character=true;
		in_stream.get(character);
	}
	} while (! in_stream.eof());
	in_stream.close();
}

An alternate way that might work for you as suggested above...

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

using namespace std;
int main()
{
    ifstream istrm;

    istrm.open("proj-8.dat");

    char ch;

    while( istrm.get(ch) )
    {
        if( ch == ' ' )
        {
            cout << ch;
            // strip extra space
            while( istrm.get(ch) && ch == ' ' )
                ;
        }

        if( istrm.good() )
            cout << ch;
    }

    istrm.close();
}

In your version you are mixing types, not sure what that will do. For example you are assigning bool to char and comparing char to bool, might want to fix that.

char character;
character=false;

character == false

An alternate way that might work for you as suggested above...

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

using namespace std;
int main()
{
    ifstream istrm;

    istrm.open("proj-8.dat");

    char ch;

    while( istrm.get(ch) )
    {
        if( ch == ' ' )
        {
            cout << ch;
            // strip extra space
            while( istrm.get(ch) && ch == ' ' )
                ;
        }

        if( istrm.good() )
            cout << ch;
    }

    istrm.close();
}

I see about setting the character to bool problem. I'll fix that. The other way worked and of course I feel dumb for not thinking of it. I really do look at these for hours before I post questions. I dont have a brain for these things.

Anyway, my only question is what "strm.good()" actually says. We haven't used that yet so I switched the statement simply to if ( ch != ' ') and it worked fine. But still, what exactly does the member function .good say?

Thank you for your help.

This can you you more information than I can:
http://www.cplusplus.com/reference/iostream/istream/

Basically, error checking is a big part of writing programs. If your reading ch by ch, you need to ensure stream is still valid. Stuff can go bad a zillion different ways... at the worst possible time!!!

This can you you more information than I can:
http://www.cplusplus.com/reference/iostream/istream/

Basically, error checking is a big part of writing programs. If your reading ch by ch, you need to ensure stream is still valid. Stuff can go bad a zillion different ways... at the worst possible time!!!

Thanks. I will take a look at that site.

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.