Let me start by saying this is my first post here at daniweb and I am pretty new at C++. With that said I will dive right in.
I have a program due next week and trying to get it hammered out. I have to create a line editor that will keep entire text on a linked list one line in a seperate node. ( I think I have that part). It starts with "Edit" and enter the file name to open. ( I think I have that as well). afte this line a prompt should appear with line number (i.e. 1>) if the letter 'I' is entered with a number 'n' following it , then insert the text followed before line 'n'. If 'd' is entered with two numbers 'n' and 'm', one 'n' or no number following it, then delete lines 'n' - 'm', line 'n', or current line. Do the same with command 'L' for listing line. If 'A' is entered then append to existing line, and finally if 'E' exit and save in text file.
I have most of the functions put together, just not real sure how to approach the prompts and such for the insert, delete, list, and append. (or exit for that matter). My code is below. I appreciate any help with this.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
struct node{
node *next;
string sentence;
//char sentence[80];
};
void append (struct node **, string);
//void append (struct node **, char);
node *add_node(node *list, string x){ //insert at front or in front of current line
//node *add_node(node *list, char x){
node *p = new node;
p->next = list;
p->sentence = x;
return p;
}
node *find_item(node *list, string x){
//node *find_item(node *list, char x){
node *p = list;
while (p != NULL){
if (p->sentence == x)
return p;
p = p->next;
}
return NULL;
}
node *find_item_before(node *list, string x){
//node *find_item_before (node *list, char x){
node *p = list, *q = list;
while (q != NULL){
if (q->sentence == x)
return p;
p = q;
q = q->next;
}
return NULL;
}
node *delete_node (node *list, string x){ //delete line
//node *delete_node (node *list, char x){
node *p = find_item_before(list, x);
if (p != NULL){
node *q;
if ((p == list) && (p->sentence == x)){
q = p->next;
delete p;
return q;
}else{
q = (p->next)->next;
delete p->next;
p->next = q;
}
}else
return list;
return list;
}
node *insert_after (node *list, string x, string y){
//node *insert_after (node *list, char x, char y){
node *p = find_item(list, x);
if (p !=NULL){
node *q = p->next;
p->next = new node;
(p->next)->next = q;
(p->next)->sentence = y;
return p->next;
}
return NULL;
}
void append (struct node **p, string x)
//void append (struct node **p, char x)
{
struct node *tmp, *r;
if (*p == NULL)
{
tmp = new node;
tmp->sentence = x;
tmp->next = NULL;
*p = tmp;
}else{
tmp = *p;
while (tmp->next !=NULL)
tmp = tmp->next;
r = new node;
r->sentence = x;
r->next = NULL;
tmp->next = r;
}
}
void print_list(node *list){ // print lines
node *p = list;
int l = 1;
//cout << l << ">";
while (p != NULL){
cout<< l << ">";
cout << p->sentence << endl;
p=p->next;
++l;
}
//cout << "tail." << endl;
return;
}
int main(){
char fName[20];
cout << "Edit: ";
cin >> fName;
ofstream outfile;
outfile.open(fName,ios::out);
if (outfile.fail())
outfile.open(fName,ios::out);
string temp;
//char temp[80];
node *lst = new node;
cout << "1> ";
cin >> temp;
getline (cin, temp);
//cin >> temp;
lst->sentence = temp;
//lst->sentence = "The first sentence";
lst->next = NULL;
print_list(lst);
/*cout << "now i'll add a sentence to the front" << endl;
lst = add_node(lst, "try here");
print_list(lst);
cout << "try and add another sentence to the front" << endl;
lst = add_node(lst, "another sentence");
print_list(lst);
cout << "try and append sentence" << endl;
append(&lst, "to this sentence");
print_list(lst);
cout << "try and delete node" << endl;
lst = delete_node(lst, "try here");
print_list(lst);
cout << "try and delete another node" << endl;
lst = delete_node(lst, "try again");
print_list(lst);*/
delete lst;
cout << "done" << endl;
cin.ignore();
return 0;
}