14 Replies - 781 Views - Last Post: 10 March 2011 - 03:33 PM Rate Topic: -----

#1 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 12:53 PM

Trying to have this code loop though the text file until there is only one whitespace in between the words. But it doesn't and skips doesn't touch any of the write space this is what i have so far
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void check_blank(ifstream& instream, ofstream& outstream);// a function to delete space


int main() 
{
  ifstream instream;
  ofstream outstream;
  instream.open("lines.txt");         //Open file: text to be edited
  outstream.open("linesout.txt");       //Open file: edited text will go
  check_blank(instream, outstream);  
  system("PAUSE");
  instream.close();                         //Close both files
  outstream.close();
  
  return 0;
}                                                  //End main

void check_blank(ifstream& instream, ofstream& outstream)
{
 char a;                                 
int count  = 0; 
int words  = 0;
while(! instream.eof())                  //While the file isn't at the end
 {
   instream.get(a);
   if(a == ' ') 
   {  
        count++;
		   if(count >= 2)      //If there are 2 or more spaces then do this
        {
           outstream <<' ';
           count = 0;                         //reset count
        }
   }
   else
	{
     outstream << a;  
	}

     
    if ( a == ' ' || a == '\n' || a == '\t' ) 
	{
       words++; 
	}
}
	 cout<< "The number of words are: "<<words<<endl;
 }

Attached File(s)


This post has been edited by StormRonin: 10 March 2011 - 01:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: loop though text file until only one whitespace bewteen words

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 12:58 PM

So what is wrong with your code?

Does it compile without error/warning messages?

If it does compile, what do you input into the program?

What does the program output?

What do you expect the program to output?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:10 PM

i edit so it said the problem
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:23 PM

Is there any reason in particular that you are reading individual characters instead of using the stream extraction operator (>>) to get a word at a time? This extraction operator will ignore whitespace.

Jim

This post has been edited by jimblumberg: 10 March 2011 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:25 PM

we were told not to
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:32 PM

   if(a == ' ') 
   {  
        count++;
		   if(count >= 2)      //If there are 2 or more spaces then do this
        {
           outstream <<' ';
           count = 0;                         //reset count
        }
   }


Shouldn't you be checking for any of your white space characters? Also wouldn't it be better to remove the extra spaces in a loop here?


Jim
Was This Post Helpful? 0
  • +
  • -

#7 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:41 PM

Removing the extra space is I what it do before I add the white space characters
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 01:48 PM

Your code will only remove every other white space character, at best. You keep resetting your counter if it is over 2 and you print a blank character then. You should set your counter when you see the first ws , print a space and then don't print anything until you see a non ws character.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 02:06 PM

So your saying keep counting until it hit a non white space then print a blank character and then rest the count
how would that look in code

This post has been edited by StormRonin: 10 March 2011 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 02:28 PM

The following should get you going.
   instream.get(a);
   if(a == ' ') // Check for all ws characters.
   {  
       if(count == 0)
       {
          count++;
          // print the space
          // increment the word counter here
       }
    }
    else
    {
        count = 0;
        // print the character
    }       



The only problem with this setup is it may not work properly if there are leading spaces.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 02:55 PM

ok it works but not all the way this is what I get now

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 03:02 PM

Please post your current code.

Are you checking for all of your ws characters? It looks like some of those are tabs.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 03:06 PM

I think for did,but not put in a Checker in the first place
while(! instream.eof())                  //While the file isn't at the end
 {
   instream.get(a);
   if(a == ' '|| a == '\n' || a == '\t')
   {
     
    if(count == 0)
    {
       count++;
       outstream <<' ';
       if ( a == ' ' || a == '\n' || a == '\t' ) 
	{
       words++; 
	}
    }
   }
else
 {
     count = 0;
	 outstream<<a;
     // print the character
 }       

This post has been edited by StormRonin: 10 March 2011 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#14 StormRonin  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 62
  • Joined: 27-May 10

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 03:23 PM

ok I found what was the problem thanks for your help

This post has been edited by StormRonin: 10 March 2011 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: loop though text file until only one whitespace bewteen words

Posted 10 March 2011 - 03:33 PM

This seems close to me:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void check_blank(ifstream& instream, ofstream& outstream);// a function to delete space


int main()
{
   ifstream instream;
   ofstream outstream;
   instream.open("lines.txt");         //Open file: text to be edited
   outstream.open("linesout.txt");       //Open file: edited text will go
   check_blank(instream, outstream);
   system("PAUSE");
   instream.close();                         //Close both files
   outstream.close();

   return 0;
}                                                  //End main

void check_blank(ifstream& instream, ofstream& outstream)
{
   char a;
   int count  = 0;
   int words  = 0;
   while(! instream.eof())                  //While the file isn't at the end
   {
      instream.get(a);
      if(a == ' '|| a == '\n' || a == '\t')
      {

         if(count == 0)
         {
            count++;
            outstream <<' ';

            words++;

         }
      }
      else
      {
         count = 0;
         outstream<<a;
         // print the character
      }

      cout<< "The number of words are: "<<words<<endl;
   }

}



I'm not sure about the word count or if you are handling the newlines correctly.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1