Setting string to set number of charecters

Hello,
I have some homework and I needed some help with finishing my code.
The question is this:
Create a program that reads a text file, named intext.txt, and writes an edited version of the file, named outtext.txt. The output file will contain the same text but each line will be as long as possible but not longer than a user specified maximum length that would be typed in from the keyboard. Words are added to the line until the addition of the next word would produce a line longer than the maximum length. This will create columns of text in the output file.

My code reads all of the words individually from the text file but I want it to read up to the word which are in the set boundaries. (so in my case one sentence will contain 20 characters or less depending on the next word.)
An example of what the output should look like is this,
Maximum length: 20 //not worried about this part yet

Create a program
that reads a text
file and outputs an
edited version of
the file.

Just to make it easy my file text contains the same text as the example I gave above.
My code:

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
  #include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int length=0;
    string inputStr;
    string buf; // Have a buffer string
    stringstream s(inputStr); // Insert the string into a stream
    ifstream InFile("intext.txt"); // just a text file
    if (InFile.fail())
    {
        cout << "File: "<< "intext.txt" << " not found"<<endl;
    }
    else
    {
        while(getline(InFile,inputStr)) // getline returns false at EOF
        {
            cout << inputStr << endl;
            int field=0;
            s<<inputStr;//put the line into the stream
            string input;//string for the input (i.e. name, ID, etc.)
            while (getline(s, input, ' '))
            {
                field++;
                cout<<"field "<<field<<" is "<<input<<endl;
                length=length+input.length()+1;
                cout<<length<<endl;
                /*  for(length=0; length<20; length++)
                {
                    cout<<input;
                }
                */
            }

        }

    }
    return 0;
}
Last edited on
can anyone suggest anything?
You read (next) word. You do know its length.
You have a sentence. You do know its length.
If appending the current word would make the sentence too long, then you print the sentence and clear it.
Append the word to the sentence.
Rinse and repeat.
I didn't quite understand what you said can you give me an example?
Bump any ideas please?
Something like:
1
2
3
4
5
6
7
8
9
while ( std::cin >> word ) {
  if ( ! (sentence.size() + word.size() < maxcol) ) {
    std::cout << sentence << '\n';
    sentence = word;
  }
  else {
    sentence += ' ' + word;
  }
}
Hello so quick update I am able to make each sentence a set number of words. I can't figure out where I need to add an end line to create a new line that will work for the code. Due to the while loop it is creating a new line after each word.

This is my current code. I am close to being done if someone can please guide me.

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
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int length=0,maxnum=0;
    cout<<"Maximum Length: ";
    cin>>maxnum;
    // process a string with fields separated by commas
    string inputStr;
    string buf; // Have a buffer string
    stringstream s(inputStr); // Insert the string into a stream
    ifstream InFile("intext.txt"); // just a text file
    if (InFile.fail())
    {
        cout << "File: "<< "intext.txt" << " not found"<<endl;
    }
    else
    {
        while(getline(InFile,inputStr)) // getline returns false at EOF
        {
            cout << inputStr << endl;
            int field=0;
            s<<inputStr;//put the line into the stream
            string input;//string for the input (i.e. name, ID, etc.)
            while (getline(s, input, ' '))//gets a string from the stream up the next space
            {
                field++;
                length=length+input.length()+1;

                if (length<maxnum)
                {
                    cout << input<<" ";
                }

                if(length<(maxnum+maxnum) && length>maxnum)
                {
                    cout << input<<" ";
                }


                if(length<(maxnum+maxnum+maxnum) && length>35)
                {
                    cout << input<<" ";
                }


                if(length<80 && length>60)
                {
                    cout << input<<" ";
                }
                if(length<100 && length>80)
                {
                    cout << input<<" ";
                }
            }
        }
    }

    return 0;
}
Topic archived. No new replies allowed.