#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;
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){
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);
}
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.
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.
#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>
usingnamespace 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;
friendclass 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) {
returntrue;
} elsereturnfalse;
}
// 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(""));
}
elsereturn *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.
elseif (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();
}
} elseif (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);
}
} elseif (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);
}
elseif (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
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.
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
// 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(""));
}
elsereturn *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