Its not working correctly believe problem is in the main function

#include <iostream>
#include <fstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator> //googled these systems from here on down
#include <stdio.h>
#include <stdlib.h>
#include<string>
using namespace std;



class Line {

public:

Line();
Line(string c);
string get_line() const;
void set_line(string c);

private:

string line;

};

Line::Line()

: line("") {}

Line::Line(string c)

: line(c) {}

string Line::get_line() const {

return line;
}

void Line::set_line(string c){

line = c;
}

//Linked List is a list of nodes so this class is neccessary.
class Node {

public:

Node(Line n);
Line get_pro();

private:

Node *next;
Line pro;

friend class TiedList; // this is what access and stores the nodes
};

Line Node::get_pro(){

return pro;
}

//This is where the nodes are stored into a linked list.
class TiedList {

public:

TiedList();
TiedList(const TiedList &origin);
~TiedList();

TiedList &operator=(const TiedList &other);

bool empty() const;
int size() const;

Node get(int collection);
//Add a new line to the beginning of the list.
void push_front(Line pro);
//I had trouble figuring out push back so I used pop front instead.
void pop_front();
void push_back(Line l);
void remove(int ps);
void insert(string line_in,int ps);

private:

Node *copy_node(const Node *in) const;

Node *first;

};


Node::Node(Line n)

: next(NULL), pro(n) {}

//Creates an empty list.

TiedList::TiedList() {

first = NULL;
}
//Tests if the list is empty.
bool TiedList::empty() const {

if (first == NULL) {

return true;
}
else
return false;
}

//Figures out the size of the list.
int TiedList::size() const {

int i = 0;
for(Node *car = first; car != NULL; car = car->next) {

i++;

}

return i;
}
Node TiedList::get(int collection) {
//Returns the line at the necessary spot.
Node* car = first;
for(int i=0; i < collection; i++) {

car = car->next;

return Node(Line(""));
}

if(car == NULL) {

return Node(Line(""));

}

else

return *car;
}
//Adds a new line at the beginning of the list.
void TiedList::push_front(Line pro) {

Node *to_insert = new Node(pro);

to_insert->next=first;

first = to_insert;

}
//Inserts a line at a specific spot.
void TiedList::insert(string lineinsert, int ps) {

Node *prev = NULL, *car = first;

Node *NewNode = new Node(lineinsert);

int position = 0;
//Making sure the list is not empty.
if(first!=NULL)
{
while(car->next != NULL && position != ps)
{

prev = car;

car = car->next;

position++;

if(ps==0)
{
first = NewNode;//first is just the head.
}//Easier for me to picture this way and draw out.
else if(car->next==NULL && ps==position+1)
{
car->next=NewNode;
NewNode->next=NULL;
}
else
{
prev->next=NewNode;
NewNode->next=car;
}
}
}
else
{
first=NewNode;
}
}
//removes a node found in my C++ book as a refresher.
void TiedList::remove(int ps){


Node *temp1 = first, *del=NULL;

int position = 0;

if(ps==1){

first = temp1->next;
delete temp1;
return;
}

for(int i=0; i < ps-2; i++) {

temp1=temp1->next;
Node *temp2 = temp1->next;
temp1->next = temp2->next;
delete temp2;
}
}

void TiedList::pop_front() {
//Deletes the first spot
if(first==NULL){

return;
}
Node *to_dele = first;
first = to_dele->next;
delete to_dele;

}

TiedList::TiedList(const TiedList &origin) {

first = copy_node(origin.first);
}
//copies the nodes
Node *TiedList::copy_node(const Node *in) const {

if(in == NULL){

return NULL;
}
Node *copy = new Node(in->pro);
copy->next=copy_node(in->next);
return copy;
}
//Destructor of linked list
TiedList::~TiedList(){

while(!empty()){

pop_front();
}
}
//just an operator to assist in the linked list.
TiedList &TiedList::operator=(const TiedList &other){

if(this == &other){

return *this;
}

while(!empty()){

pop_front();
}

first = copy_node(other.first);

return *this;

}
//The main function
int main(int argc, char *argv[]) {

cout << "Michael editor.." << endl;
cout << "Please type a few lines of text to start with." << endl;

cout << "Click ctrl-d to stop typing and to save the file " << endl;

cout << "I n -> insert the text to line n." << endl;
cout << "D n -> delete line n." << endl;
cout << "L -> list all the lines." << endl;

cout << "Q -> save into a file and quit." << endl;


//TiedList is the name of the Linked List.

TiedList tiedlist;

string input;
string editline;
int insert_index = 0;
vector<Line> reverse2;
bool flag = true;
string line;


if(argc != 2){

return 1;

}

else

string myEditor = argv[1];
ifstream inputfile(argv[1]);



if(inputfile.is_open())
{ //puts all the lines in a vector.

while(getline(inputfile,line))
{

line+="\n";
Line nodeline(line);

reverse2.push_back(line);
cout << line;
}

}
//puts the list in the correct order
for(int i = reverse2.size()-1; i>=0; i--)
{

tiedlist.push_front(reverse2[i]);
cout << endl;
}

for(int i = 0; i < tiedlist.size(); i++)
{
//Checks the inputs from the user.
cout << i+1 << "> " << tiedlist.get(i).get_pro().get_line();
}

while(line.c_str()[0] != 'Q')
{
cout << tiedlist.size()+1 << "> ";
}

getline(cin,line);
//easy for user inputs
if(line.c_str()[0] == 'L')
{
for(int i = 0; i < tiedlist.size(); i++)
{
cout << i+1 << "> " << tiedlist.get(i).get_pro().get_line();
}
}
else if(line.c_str()[0] == 'I')
{
int i = 2;
string number = "";
char num = line.c_str()[2];
while(line.c_str()[i] != '\0')
{
if(isdigit(num))
{
number+=line.c_str()[i];
}
i++;
}

num = atoi(number.c_str());
int i_index = num;
string to_insert = "";

cout << i_index << "> ";
//input safety nets
if(i_index > tiedlist.size() || i_index < 1)
{
cout << "The line number is out of range." << endl;
}
else
{
getline(cin,to_insert);
to_insert += '\n';
tiedlist.insert(to_insert,i_index-1);
}
}
else if(line.c_str()[0] == 'D')
{
int i = 2;
string number = "";
char num = line.c_str()[2];
//A loop checking the line and input
while(line.c_str()[i] != '\0')
{

if(isdigit(num))
{
number+=line.c_str()[i];
}
i++;
}

num = atoi(number.c_str());

int i_index = num;
if(i_index > tiedlist.size() || i_index < 1)
{
cout << "The line number is out of range." << endl;
}
else
tiedlist.remove(i_index);
}

else if(line.c_str()[0] == 'Q')
{
return 0;
}
else
{
cout << "Invalid input" << endl;
cout << "Unable to open file." << endl;
}

cout << "Thanks for using my editor." << endl;
//output the linked list
inputfile.close();
ofstream outputfile(argv[1]);

if(outputfile.is_open()){

for(int i = 0;i < tiedlist.size(); i++){

outputfile << tiedlist.get(i).get_pro().get_line();

outputfile.close();
}

}


}
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

What do you mean by "doesn't work"? Does it crash? Does it sabotage your game of chess? Does it disable your grandmother's stair lift? You need to be specific.
It's running in an infinite loop it seems. When I execute it all it does is display ^1 over and over again
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

Did I do what you asked still new to this website
http://www.cplusplus.com/articles/z13hAqkS/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <iostream>
#include <fstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator> //googled these systems from here on down
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

class Line {

public:
	Line();
	Line(string c);
	string get_line() const;
	void set_line(string c);

private:
	string line;
};

Line::Line()

	: line("") {}

Line::Line(string c)

	: line(c) {}

string Line::get_line() const { return line; }

void Line::set_line(string c) { line = c; }

// Linked List is a list of nodes so this class is neccessary.
class Node {

public:
	Node(Line n);
	Line get_pro();

private:
	Node *next;
	Line pro;

	friend class TiedList; // this is what access and stores the nodes
};

Line Node::get_pro() { return pro; }

// This is where the nodes are stored into a linked list.
class TiedList {

public:
	TiedList();
	TiedList(const TiedList &origin);
	~TiedList();

	TiedList &operator=(const TiedList &other);

	bool empty() const;
	int size() const;

	Node get(int collection);
	// Add a new line to the beginning of the list.
	void push_front(Line pro);
	// I had trouble figuring out push back so I used pop front instead.
	void pop_front();
	void push_back(Line l);
	void remove(int ps);
	void insert(string line_in, int ps);

private:
	Node *copy_node(const Node *in) const;

	Node *first;
};

Node::Node(Line n)

	: next(NULL), pro(n) {}

// Creates an empty list.

TiedList::TiedList() { first = NULL; }
// Tests if the list is empty.
bool TiedList::empty() const {

	if (first == NULL) {

		return true;
	} else
		return false;
}

// Figures out the size of the list.
int TiedList::size() const {

	int i = 0;
	for (Node *car = first; car != NULL; car = car->next) {

		i++;
	}

	return i;
}
Node TiedList::get(int collection) {
	// Returns the line at the necessary spot.
	Node *car = first;
	for (int i = 0; i < collection; i++) {

		car = car->next;

		return Node(Line(""));
	}

	if (car == NULL) {

		return Node(Line(""));

	}

	else

		return *car;
}
// Adds a new line at the beginning of the list.
void TiedList::push_front(Line pro) {

	Node *to_insert = new Node(pro);

	to_insert->next = first;

	first = to_insert;
}
// Inserts a line at a specific spot.
void TiedList::insert(string lineinsert, int ps) {

	Node *prev = NULL, *car = first;

	Node *NewNode = new Node(lineinsert);

	int position = 0;
	// Making sure the list is not empty.
	if (first != NULL) {
		while (car->next != NULL && position != ps) {

			prev = car;

			car = car->next;

			position++;

			if (ps == 0) {
				first = NewNode; // first is just the head.
			} // Easier for me to picture this way and draw out.
			else if (car->next == NULL && ps == position + 1) {
				car->next = NewNode;
				NewNode->next = NULL;
			} else {
				prev->next = NewNode;
				NewNode->next = car;
			}
		}
	} else {
		first = NewNode;
	}
}
// removes a node found in my C++ book as a refresher.
void TiedList::remove(int ps) {

	Node *temp1 = first, *del = NULL;

	int position = 0;

	if (ps == 1) {

		first = temp1->next;
		delete temp1;
		return;
	}

	for (int i = 0; i < ps - 2; i++) {

		temp1 = temp1->next;
		Node *temp2 = temp1->next;
		temp1->next = temp2->next;
		delete temp2;
	}
}

void TiedList::pop_front() {
	// Deletes the first spot
	if (first == NULL) {

		return;
	}
	Node *to_dele = first;
	first = to_dele->next;
	delete to_dele;
}

TiedList::TiedList(const TiedList &origin) { first = copy_node(origin.first); }
// copies the nodes
Node *TiedList::copy_node(const Node *in) const {

	if (in == NULL) {

		return NULL;
	}
	Node *copy = new Node(in->pro);
	copy->next = copy_node(in->next);
	return copy;
}
// Destructor of linked list
TiedList::~TiedList() {

	while (!empty()) {

		pop_front();
	}
}
// just an operator to assist in the linked list.
TiedList &TiedList::operator=(const TiedList &other) {

	if (this == &other) {

		return *this;
	}

	while (!empty()) {

		pop_front();
	}

	first = copy_node(other.first);

	return *this;
}
// The main function
int main(int argc, char *argv[]) {

	cout << "Michael editor.." << endl;
	cout << "Please type a few lines of text to start with." << endl;

	cout << "Click ctrl-d to stop typing and to save the file " << endl;

	cout << "I n -> insert the text to line n." << endl;
	cout << "D n -> delete line n." << endl;
	cout << "L -> list all the lines." << endl;

	cout << "Q -> save into a file and quit." << endl;

	// TiedList is the name of the Linked List.

	TiedList tiedlist;

	string input;
	string editline;
	int insert_index = 0;
	vector<Line> reverse2;
	bool flag = true;
	string line;

	if (argc != 2) {

		return 1;

	}

	else

		string myEditor = argv[1];
	ifstream inputfile(argv[1]);

	if (inputfile.is_open()) { // puts all the lines in a vector.

		while (getline(inputfile, line)) {

			line += "\n";
			Line nodeline(line);

			reverse2.push_back(line);
			cout << line;
		}
	}
	// puts the list in the correct order
	for (int i = reverse2.size() - 1; i >= 0; i--) {

		tiedlist.push_front(reverse2[i]);
		cout << endl;
	}

	for (int i = 0; i < tiedlist.size(); i++) {
		// Checks the inputs from the user.
		cout << i + 1 << "> " << tiedlist.get(i).get_pro().get_line();
	}

	while (line.c_str()[0] != 'Q') {
		cout << tiedlist.size() + 1 << "> ";
	}

	getline(cin, line);
	// easy for user inputs
	if (line.c_str()[0] == 'L') {
		for (int i = 0; i < tiedlist.size(); i++) {
			cout << i + 1 << "> " << tiedlist.get(i).get_pro().get_line();
		}
	} else if (line.c_str()[0] == 'I') {
		int i = 2;
		string number = "";
		char num = line.c_str()[2];
		while (line.c_str()[i] != '\0') {
			if (isdigit(num)) {
				number += line.c_str()[i];
			}
			i++;
		}

		num = atoi(number.c_str());
		int i_index = num;
		string to_insert = "";

		cout << i_index << "> ";
		// input safety nets
		if (i_index > tiedlist.size() || i_index < 1) {
			cout << "The line number is out of range." << endl;
		} else {
			getline(cin, to_insert);
			to_insert += '\n';
			tiedlist.insert(to_insert, i_index - 1);
		}
	} else if (line.c_str()[0] == 'D') {
		int i = 2;
		string number = "";
		char num = line.c_str()[2];
		// A loop checking the line and input
		while (line.c_str()[i] != '\0') {

			if (isdigit(num)) {
				number += line.c_str()[i];
			}
			i++;
		}

		num = atoi(number.c_str());

		int i_index = num;
		if (i_index > tiedlist.size() || i_index < 1) {
			cout << "The line number is out of range." << endl;
		} else
			tiedlist.remove(i_index);
	}

	else if (line.c_str()[0] == 'Q') {
		return 0;
	} else {
		cout << "Invalid input" << endl;
		cout << "Unable to open file." << endl;
	}

	cout << "Thanks for using my editor." << endl;
	// output the linked list
	inputfile.close();
	ofstream outputfile(argv[1]);

	if (outputfile.is_open()) {

		for (int i = 0; i < tiedlist.size(); i++) {

			outputfile << tiedlist.get(i).get_pro().get_line();

			outputfile.close();
		}
	}
}
provide an example of the input.


If you run your code through a debugger, you may interrupt the program (ctrl-c) when it's printing the ^1 so you can see what line causes it and how did you get there
The program is suppose to be able to insert a line at a GIVEN LINE NUMBER and then also delete lines etc.
your program requires a filename as an argument, then it reads from that file. ¿how do you expect me to test your code if I don't know the content of the file?

went and used the source code as input file, and got "294> " repeated over and over again.
300
301
302
	while (line.c_str()[0] != 'Q') {
		cout << tiedlist.size() + 1 << "> ";
	}
that's an infinite loop, the condition refers to `line' but there is nothing in the body that affects `line'
Last edited on
The file he is using has this in it.

testfile

1
2
3
4
I3


Q
The file is a blank file your creating when you call the executable file ./myEditor. This is the c++ portion you execute it in a bash program which calls ./myEditor testfile. test file being the blank file that it creates. Is the way it is explained. But when I run it all it displays is the options I, Q etc and when I select an option it doesn't work
¿you are trying to read from an empty file?
1
2
3
4
		// input safety nets
		if (i_index > tiedlist.size() || i_index < 1) {
			cout << "The line number is out of range." << endl;
		} else
on your "I" command, you check that the number is valid. If the list is empty, then no number is valid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Node TiedList::get(int collection) {
	// Returns the line at the necessary spot.
	Node *car = first;
	for (int i = 0; i < collection; i++) {
		car = car->next;
		return Node(Line("")); //¿?
	}

	if (car == NULL) {
		return Node(Line(""));
	}

	else
		return *car;
}
except for .get(0) that function would return an empty line because of that unconditional return.
1
2
3
4
5
for(int K=0; K<collection; ++K){
   if( car==NULL ) 
      return Node(Line("")); //the list is too small, the index is outside the range
   car = car->next;
}



368
369
370
371
372
373
374
375
376
	if (outputfile.is_open()) {

		for (int i = 0; i < tiedlist.size(); i++) {

			outputfile << tiedlist.get(i).get_pro().get_line();

			outputfile.close(); //¿?
		}
	}
you output one line and then closed the file
1
2
3
4
5
6
	if (outputfile.is_open()){
		for (int i = 0; i < tiedlist.size(); i++){
			outputfile << tiedlist.get(i).get_pro().get_line();
		}
	//outputfile.close(); //not even needed, the destructor will take care
	}



> Is the way it is explained.
It was explained to you. We are not in your class, so make an effort and try to describe the situation properly
Topic archived. No new replies allowed.