Random spaces and "-" being inserted in wrong spots

My assignment for class is to create a program that reads a user's file and formats it to 60characters per line. Each word must be evenly spaced so that "hello my name is" gets changed to "hello my name is". if the letter "t" from the word "astronaut" is the 60th character of line, replace the "t" with a "-" and start a new line. The only issue im having is that the first character of some lines are starting with a space rather than a letter and "-" is being inserted in some lines where it is not needed. Any suggestion on how to fix this?

Also for testing purposes im just using a random quote to test my code, not reading from the file that our prof will soon upload.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
         //
//  main.cpp
//  CopyArray
//
//  Created by Alek Gulbenkian on 9/22/16.
//  Copyright © 2016 Alek Gulbenkian. All rights reserved.
//

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, const char * argv[])
{
    char *p, *b, test[] = "The thousand     injuries of Fortunato I had borne as I best could,      but when he ventured upon insult I vowed revenge. You, who so well know the nature of my soul, will not suppose, however, that gave utterance to a threat. At length I would       be avenged; this was a point definitely, settled but the very definitiveness with      which it was resolved precluded        the idea of risk.       I must not only punish but punish with impunity.        A wrong is unredressed when retribution overtakes its redresser. It is equally unredressed when the avenger fails to make       himself felt as such       to him who has done the wrong." ;
    
   // cout<< "Enter the amount of char per length you would want: ";
    
    //int length = 60;
   // cin>>length;

    p = b =test;
    if(p) do
    {
        while(*p == ' ' && *(p+1) == ' ')
            p++;
    }
    while((*b++ = *p++));
    
    int counter =0;
    
    char *z = test;
    for(int i =0; z[i]; i++)
    {
        if(counter ==0 && isspace(z[i]))
            z[i] = z[i+1];
        else
        {
            counter ++;
            if(counter == 60 && !isspace(z[i-1]))
            {
                cout<<"-";
                cout<<"\n";
                counter =0;
            }
            else if(counter ==60 && isspace(z[i]))
            {
                
                cout<<"\n";
                counter =0;
            }
            else if(counter ==60)
            {
                cout<< "\n";
                counter =0;
            }
            
            
        }
        
        cout<<z[i];
    }
    
    cout<<endl;
    
   // cout<<test<<endl;;   
   
}



code runs: 
The thousand injuries of Fortunato I had borne as I best co-
uld, but when he ventured upon insult I vowed revenge. You, 
who so well know the nature of my soul, will not suppose, ho-
wever, that gave utterance to a threat. At length I would be-
 avenged; this was a point definitely, settled but the very 
definitiveness with which it was resolved precluded the idea-
 of risk. I must not only punish but punish with impunity. A-
 wrong is unredressed when retribution overtakes its redress-
er. It is equally unredressed when the avenger fails to make-
 himself felt as such to him who has done the wrong.
Last edited on
> the first character of some lines are starting with a space rather than a
> letter and "-" is being inserted in some lines where it is not needed.
take a close look at those some lines, to notice where it happens
1
2
3
4
5
6
(...)At length I would be-
 avenged; 
(...)impunity. A-
  wrong 
(...)fails to make-
 himself felt 
The word had ended, you were at the space after, but decided to hyphenate it.
Now read your conditions at line 40 and 46, and realize why 46 can never happen.
Last edited on
Thank you!! so i changed the for loop to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(int i =0; z[i]; i++)
    {
        counter2++;
        
        if(counter== userLength)
        {
            if(isspace(z[i-1]))
            {
                cout<<"\n";
                counter =0;
            }
            else if(!isspace(z[i+1]) && !isspace(z[i]))
            {
                cout<<"-";
                cout<<"\n";
                counter=0;
            }
            counter=0;
        }
        
        counter++;
        
        cout<<z[i];


and it worked !!
Topic archived. No new replies allowed.