Simple text editing program

I got a little bit of trouble creating a program that edits text in C++. Keep in mind that I'm still at the beginning of programing. Here is what I must do:

Some text is placed in text file F. I must write a program-editor, which based on the file F, commands and data from the keyboard creates the file FF. The program-editor must recognize and process the following commands:

AN - insert text after the n-th row;

IN - insert text before the n-th row;

CM,N - change text of the rows from m-th to n-th;

DM,N - deleting the rows from m-th to n-th;

E - end of editing;

where m and n are the number of rows in the file F.The commands are recorded one on row and are made as a menu.

So the only problem I'm facing is the DELETING command. It works alone fine, but used parallel with the other commands, the program crashes. Here is the 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    #include <iostream>
    #include <conio.h>
    #include <fstream>
    #include <string>
    using namespace std; 

	const int N=255;

	void change(string mas[]);
	void del (string mas[]);
	void insertB (string mas[]);          
	void insertA (string mas[]);



    int main()                             
    {   
		int ans;     
		ifstream file("F.txt");   
		ofstream file2("FF.txt");
		file2<<file.rdbuf();
		file.close();
		file2.close();

		do {                    
		string massive[N];     
        string prom;
        ifstream file;
        file.open("FF.txt" , ios::in);
        int i=0;
        while(!file.eof())
        { getline(file, prom);
          massive[i]=prom;
		  i++;}
		    cout<<"===========MENU==========="<<endl;    
			cout<<"    Make your choise:"<<endl;
			cout<<"1.Insert text before n-th row."<<endl;
			cout<<"2.Insert text after n-th row."<<endl;
			cout<<"3.Change text in the rows from n-th to m-th."<<endl;
			cout<<"4.Delete the rows from n-th to m-th."<<endl;
			cout<<"5.End of editing."<<endl;
			
			cin>>ans;
			switch(ans){            
			case 1: insertB (massive);break;
			case 2: insertA (massive);break;
			case 3: change (massive);break;
			case 4: del (massive);break;
			case 5: exit(1);
			}
		}
		while(ans!=5);
	}

	void change (string mas[]){         
		int n,m;
		cout<<"Please enter the rows you want to be changed !"<<endl;
		cout<<"From :";
		cin>>n;
		n=n-1;   
		cout<<endl<<"To :";
		cin>>m;
		m=m-1; 
		cin.ignore(INT_MAX, '\n'); 
		for (n ; n<=m ; n++){       
			cout<<endl<<"Enter text for ("<<n+1<<")row: "<<endl;
			getline(cin,mas[n]);
		}
		ofstream file2("FF.txt",ios::in); 
		for (int i=0 ; i<=N; i++){
		file2<< mas[i]<<endl;
		}
	}

	void del (string mas[])    
	{
		int n,m;
		cout<<"Please enter the rows you want to delete "<<endl;
		cout<<"From :";
		cin>>n;
		n=n-1;   
		cout<<endl<<"To :";
		cin>>m;
		m=m-1;    
		for ( n ; n<N-n ; n++)  
		{
			m++;
			mas[n]=mas[m];
		}
		ofstream file2("FF.txt",ios::in); 
		for (int i=0 ; i<=N; i++){
		file2<< mas[i]<<endl;
		}

	}

	void insertB (string mas[]) 
	{
		int n;
		string swap[N];
		for (int i=0 ; i<N ; i++){ 
			swap[i]=mas[i];
		}
		cout<<"Please enter the row you want to add text before!"<<endl;
		cin>>n;
		n=n-1;
		cout<<"Enter text for the row : ";
		cin.ignore(INT_MAX, '\n');
		getline (cin, mas[n]);    
		for ( n ; n<N ; n++){
			mas[n+1]=swap[n];    
		}
		ofstream file2("FF.txt",ios::in);  
		for (int i=0 ; i<=N; i++){
		file2<< mas[i]<<endl;
	}
	}

	void insertA (string mas[]) {
		int n;
		string swap[N];
		for (int i=0 ; i<N ; i++){
			swap[i]=mas[i];
		}
		cout<<"Please enter the row you want to add text after!"<<endl;
		cin>>n;
		cout<<"Enter text for the row : ";
		cin.ignore(INT_MAX, '\n');
		getline (cin, mas[n]);      
		for ( n ; n<N-1 ; n++){
			mas[n+1]=swap[n];    
		}
		ofstream file2("FF.txt",ios::in); 
		for (int i=0 ; i<=N; i++){
		file2<< mas[i]<<endl;
	}
	}

Thanks in advance!
Use a std::vector<std::string> to hold the lines of text.

And then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::vector<std::string>& insert_line_before( std::vector<std::string>& text,
                                              std::size_t line_number,
                                              const std::string& new_line )
{
    if( line_number < text.size() )
        text.insert( text.begin()+line_number, new_line ) ;
    return text ;
}

std::vector<std::string>& delete_lines( std::vector<std::string>& text,
                                        std::size_t from_line_number,
                                        std::size_t to_line_number )
{
    if( to_line_number < text.size() && from_line_number <= to_line_number )
        text.erase( text.begin()+from_line_number, text.begin()+to_line_number+1 ) ;
    return text ;
}

etc.
Thanks for the fast reply. But isn't there an easier way to make it work, because I didn't think we've been using vectors for now. I mean the delete function works when I used it alone, or when I'm using it first, before the other commands. But when I decided to use for example insert row and then delete row the program crashes. Of course, I'll consider using the method with vector, but I'm really curious why it doesn't work like that. Thanks again for your time.
... but I'm really curious why it doesn't work like that.

Because of Line 91 going past the end of the array I'd imagine. The highest addressable point in an array is one less then the size it was set to because '0' is the start of the index. You're going to have the same issue on Lines 114 and 134, actually the whole "insertB()" function might be a tear down now that I look at it. It's not getting off on the right foot for what it looks like you want it to do, sorry :\.
Topic archived. No new replies allowed.