Help With replacing spaces.Amateur

This is a discussion on Help With replacing spaces.Amateur within the C++ Programming forums, part of the General Programming Boards category; Heya there, i recently started to learn the C++ language and i am trying out things. The deal here is ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Help With replacing spaces.Amateur

    Heya there,
    i recently started to learn the C++ language and i am trying out things.
    The deal here is i want to replace 4 spaces with a tab.The code seems fine to me,but its not proper as when i run it the 4 spaces are replaced by 6 spaces,5-7 and so on.I am at a dead end and any help would be appreciated.Here is my code:
    Code:
    #include <iostream>
    using namespace std;
    char x;
    int spaces=0;
    
    
    int main()
    {
    char x;
    int spaces=0;
    
    
        while(cin.get(x))
        {
         if (x !=' ')
          {
            if (spaces>0)
            {
             for (int i=0; i<spaces; i++)
              {
                cout<<' ';
               }
             spaces=0;
             }
            cout<<x;
           }
         else
            {
             if (++spaces==4)
              {
                cout<<'\t';
                spaces=0;
              }
            }
        }
    }
    Thanks for your time.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    So if 0 < spaces < 4 than you don't want to print anything until you know there aren't four spaces. Also, you want to reset thenumber of spaces counted at some point.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    No if the spaces<4 i just want it to repeat the same, and it does it.
    And what i want to do is that if there are more than 4 spaces in a row,eg. 5 spaces -> one tab and one space(i forgot to mention that in the 1st post,that what i want is to replace continuous spaces).

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    You're right, I misread. I can't see anything wrong, except that your global variables are unused. Remove those. But that's not the problem.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    yea you are right the global variables arent used,i forgot to erase them.
    Hmm as for the code i noticed that when i enter for e.g 5-7 chars and then 4 spaces->
    7chars abcdefg"4spaces"hijk it outputs abcdefg hijk
    6chars abcdefgh"4spaces"ijkl it outputs abcdefgh"2spaces"ijkl
    if i put 4 chars and then 4 spaces in a row it just repeats the same thing i inputed
    if i put 8 or more chars at first it doubles the spaces i entered
    Dunno why it brings this out maybe i missread my code or sth ?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    That sounds like the console is treating \t as a 8-space row alining tab. So to change the behavior you'd have to look at the console settings. Your program 's working fine.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    yea it treats \t as an 8-space row but in the setting says that tabs size in spaces is 4 even though it appers to be 8.Maybe if i change my spacelimit from 4 to 8 it ll work ?
    i mean where i write ++spaces==4 if i change it to ++spaces==8 will it work right ?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    I am done with the program i appreciate the help very much.thank you
    You can close this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. replacing tabs with spaces
    By marto1914 in forum C++ Programming
    Replies: 0
    Last Post: 10-03-2010, 12:44 AM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. problem for amateur lol
    By clarky101 in forum C Programming
    Replies: 3
    Last Post: 10-21-2007, 09:04 AM
  4. Amateur question.
    By RP319 in forum C++ Programming
    Replies: 11
    Last Post: 02-07-2005, 06:27 AM
  5. very amateur question:
    By jemer in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2003, 02:41 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21