My problem is that I'm using getline(cin, line) but then I dont know how to extract the numbers in the commands that COULD have number for specifying the line index.
Start the program with cout >> "EDIT file", after which a prompt appears
along with the line number. If the letter I is entered with a number n following it, then insert the text to be followed before line n. If I is not followed by a number, then insert the text before the current line. If D is entered with two numbers n and m, one n, or no number following it, then
delete lines n through m, line n, or the current line. Do the same with the command L, which stands for listing lines. If A is entered, then append the text to the existing lines. Entry E signifies exit
and saving the text in a file. Here is an Example of how is should look de output:
EDIT testfile
1> The first line
2>
3> And another line
4>I 3
3> The second line
4> One more line
5> L
1> The first line
2>
3> The second line
4> One more line
5> And another line // This is now line 5, not 3;
5> D 2 // line 5, since L was issued from line 5;
4> L // line 4, since one line was deleted;
1> The first line
2> The second line // this and the following lines
3> One more line // now have new numbers
4> And another line
4> E
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
|
#include "DLList.h"
#include <iostream>
#include <string>
#include<sstream>
using namespace std;
int main()
{
DLList<string> list;
string line, word;
stringstream ss;
int index = 1, n, m;
char c;
cout << "EDIT textfile\n";
cout << index << "> ";
getline(cin, line);
while (line.length() == 0 || line.at(0) != 'E')
{
ss << line;
if (line.length() == 0)
{
list.addToIndx(index, line);
index++;
}
else
{
switch (line.at(0))
{
case 'A':
//Append to node
cout << index << "> ";
cin >> line;
list.appendToIdx(index, line);
break;
case 'D':
//delete Node
if (line.size() == 5)
{
n = line.at(2) - 48;
m = line.at(4) - 48;
list.deleteRage(n, m);
index = n - m + 1;
}
else if (line.size() == 3)
{
n = line.at(2) - 48;
list.deleteRage(n, n);
index--;
}
else
{
list.deleteRage(index, index);
index--;
}
//index = index--;
break;
case 'I':
//Inser node
if (line.size() == 3)
{
n = line.at(2) - 48;
index = n;
cout << n << "> ";
}
else {
n = index--;
cout << n << "> ";
}
getline(cin, line);
list.addToIndx(n, line);
break;
case'L':
//List or print nodes
if (line.size() == 5)
{
n = line.at(2) - 48;
m = line.at(4) - 48;
list.printList(n, m);
}
else if (line.size() == 3)
{
n = line.at(2) - 48;
list.printList(n, n);
}
else
{
list.printList(index, index);
}
break;
default:
list.addToIndx(index, line);
index++;
break;
}//switch end
}//end else
cout << index << "> ";
getline(cin, line);
}
return 0;
}
| |