Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Intro C++

Name: Anonymous 2012-11-07 22:14

I'm trying to write a program that edits a text file to replace all consecutive spaces with a single space.  The editing of the spaces is what's giving me problems.

}

while(! fin.eof())
    {
        fin.get(next);

         if(isspace(next))
             count = count + 1;
        
         if(isspace(next) && count >= 2)
         {
            fout << ' ';
            count = 0;
         }
       
         else
         {
            fout << next;   
            count = 0;
         }
    }
    fin.close( );
    fout.close( );
       
    }

Basically, I'm trying to add 1 every time a space is encountered as some sort of comparison, when it's = 2 it gets replaced with single space.  So far it outputs the text unedited.  Any help would be appreciated.

Name: Anonymous 2012-11-07 22:33

what's with the cs 101 retard flood

is it the link on /g/ay

Name: Anonymous 2012-11-07 23:43


int main() {
 return system("sed 's/  / /g'");
}

Name: Anonymous 2012-11-08 0:40

>>3
wtf does the system function do?

Name: Anonymous 2012-11-08 1:19

Why are you resetting count and printing a space every time you encounter a second space?

Name: Anonymous 2012-11-08 1:39

Using procedural, application-programming languages for this type of work is time consuming and un-needed, use a high level scripting language for this type of work. If you have to use c++ thou, use strategically placed print statements, and make sure that the code is EXACTLY correct, read it in great detail.

Name: Anonymous 2012-11-08 2:41

the more simple way to handle the problem is to build a function that breaks up the string from the file into an array of words.

e.g.

array words(inputstring):
  str word = emptystring
  array words = emptyarray
  for character in string:
    if character is not space:
      word += character
    else:
      array.push(word)
  return words

string join_nicely(words)]
  string the_output
  for word in words:
    if word is not space:
      the_output += (word + space)
  return the_output

This all handles quadruple spaces as well and yes using C++ for this will be tedious - but should be a nice exercise to get you started.

Name: Anonymous 2012-11-08 2:46

Name: Anonymous 2012-11-08 5:12

>>7
P.S. in actual FIOC you can just go
[code]
output_string = ' '.join(input_string.split())

Name: Anonymous 2012-11-08 8:30

vim: %s/ +/ /g

Name: Anonymous 2012-11-09 11:35

check 'em

Don't change these.
Name: Email:
Entire Thread Thread List