Select a line of text and delete it

How can I select a specific line, maybe based on the description such as "Pants" or by the entry number and delete that line?

Thanks to ALL that read this. Examples are helpful. THANKS!

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int write(void)
{
    ofstream outfile;
    char description[20],
         date[20],
         wholesalecost[20],
         retailcost[20],
         quantity[20];
    int index;
    outfile.open("InventoryData.txt",ios::app);
    cout<<"Enter the Description: ";
    cin>>description;
    cin.ignore();
    cout<<"Enter the Date added into inventory: ";
    cin>>date;
    cin.ignore();
    cout<<"Enter the Wholesale Cost: ";
    cin>>wholesalecost;
    cin.ignore();
    cout<<"Enter the Retail Cost: ";
    cin>>retailcost;
    cin.ignore();
    cout<<"Enter the Quantity: ";
    cin>>quantity;
    cin.ignore();
    outfile<<description<<" "<<date<<" "<<wholesalecost<<" "<<retailcost<<" "<<quantity<<" "<<"\n"<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}


int read(void)
{
    ifstream inputfile;
    inputfile.open("InventoryData.txt",ios::in);

    char description[20],
         date[20],
         wholesalecost[20],
         retailcost[20],
         quantity[20];  
    int counter =1;
    while(!inputfile.eof())
    {
        inputfile>>description>>date>>wholesalecost>>retailcost>>quantity;
        cout<<"Description: "<<description<<endl;
	cout<<"Date entered into system: "<<date<<endl;
        cout<<"Wholesale Cost: $ "<<wholesalecost<<endl;
        cout<<"Retail Cost: $ "<<retailcost<<endl;
	cout<<"Quantity in stock:"<<quantity<<"\n"<<endl;
        counter++;
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    inputfile.close();
    return 0;
}

int change(void)   //aka delete line
{
    ifstream inputfile;
    inputfile.open("InventoryData.txt",ios::in);

//select a line by number or match text??

    
    inputfile.close();
    return 0;
}

int main()
{
      int choice;
      char ch;
      do
	  {
          cout<<endl;
	  cout<<"--------INVENTORY MANAGEMENT------------------\n";
          cout<<"\tPlease choose from the following: \n";
          cout<<"\t1. Read the current inventory data. \n";
          cout<<"\t2. Write additional inventory data. \n";
          cout<<"\t3. Change an existing inventory entry. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;
           switch(choice)
          {
             case 1:
                   read();
                   break;
             case 2:
                   write();
                   break;
             case 3:
				// change();
                   break;
             default:
                   cout << "\nInvalid choice. Exiting....\n" << endl; 
				   return 0;
           }
           cout<<"\nWould you like to continue?  (y/n)\n";
           cin>>ch;
     }
	  while(ch == 'y' || ch == 'Y');
	  return 0;
}
Last edited on
input file

Dog
Cat
Lion
My Sister
Panda
Bear


Example
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;


void openFile(ofstream &in, string fileName);
void openFile(ifstream &in, string fileName);
void deleteAnimal(ifstream &in, string animal, string inFileName);
void outputFile(ifstream &out);
int main()
{
	string fileName("Animals.txt");
	ifstream inFile;
	string animal;
	
	openFile(inFile, fileName);
	outputFile(inFile);
	inFile.close();
	openFile(inFile, fileName);

	cout << "What animal would you like to delete: ";
	getline(cin, animal);
	cout << endl;

	deleteAnimal(inFile, animal, fileName);

	outputFile(inFile);


	cin.ignore();
	return 0;
}


void openFile(ifstream &stream, string fileName)
{
	stream.open(fileName);
	if (!stream)
	{
		cout << fileName << " could not be opened";
		cin.ignore();
		exit(1);
	}


}


void openFile(ofstream &stream, string fileName)
{
	stream.open(fileName);
	if (!stream)
	{
		cout << fileName << " could not be opened";
		cin.ignore();
		exit(1);
	}

}
void deleteAnimal(ifstream &in, string animal, string inFileName)
{
	ofstream outStream;
	string fileName = "temp.txt";
	string temp;

	openFile(outStream, fileName);

	while (getline(in, temp))
	{
		if (temp.find(animal) == std::string::npos)
			outStream << temp << endl;
	}

	outStream.close();
	in.close();

	openFile(outStream, inFileName);
	openFile(in, fileName);

	while (getline(in, temp))
	{
		outStream << temp << endl;
	}

	in.close();
	outStream.close();

	openFile(in, inFileName);

}


void outputFile(ifstream &out)
{
	string temp;

	while (getline(out, temp))
	{
		cout << temp << endl;
	}
}


Output
Dog
Cat
Lion
My Sister
Panda
Bear
What animal would you like to delete: My Sister

Dog
Cat
Lion
Panda
Bear


if you need a more advanced method you'll have to wait for somebody else to answer
Last edited on
That's COOOOOOL!!!!

Thanks!!!!!!!!!!!!!
Topic archived. No new replies allowed.