new

Hi . I want to create a new line after 80 character without Enter ( create new line ) how can i solve this ???????????
When writing to what?
string . when i write a string
When writing to what? The console? A file?
Last edited on
yes in a console
In most consoles, lines wrap around after 80 characters automatically.
You mean word wrap? In a console it wraps automatically but if you want to do it here is some code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    string input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tempor libero ut lacus ultrices pellentesque. Donec sed metus in nulla viverra mollis vel nec tellus. Integer vitae nulla diam, dictum iaculis magna. Duis et auctor augue. Etiam vehicula mattis tristique. Nunc rhoncus dolor eu ipsum consectetur ut ornare nunc pellentesque. Nullam accumsan purus nec dui interdum malesuada. Nulla facilisis lacus vitae turpis consequat adipiscing. Integer at venenatis erat. Nulla vitae risus sit amet lorem facilisis volutpat imperdiet quis enim. In. ";
    cout << "Your string was " << input.length() << " characters long" << endl;
    int breaks = input.length()/80; //Number on new lines it needs
    string output;
    for(int i=0; i < breaks; i++){
        output += input.substr(80*i, 80) + "\n";  //take a string 80 characters long every 80 characters and add a new line after it
    }

    ofstream file;
    file.open("test.txt");
    file << output; //print the string with linebreaks to the file test.txt
    file.close();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.