Read it without writing it:
while (getline(kill,line))
{
if (line != deleteline )
{
temp << line << endl ;
}
else
{
getline(kill, line); // Error handling is pretty irrelevant here.
}
}
Question
I am writing a simple program where the user can:
A: add a new client
B: see list of clients
C: delete the database
D: delete a client
it works well for the most part, the problem is basically that every client also has debt. The debt is shown in the line below the client's name.
Name: bryan
Debt: 45.56$
Name: Rosa
Debt: 23.43$
Name: Jon
Debt: 55.55$
I want to make it so that when the client's name is deleted, so is the line below it, etc. What code would make this work? I'm only about 1 month into c++ so If you could please dumb it down for me to understand I would be really thankful. :)
My code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;
int kill();
int end() ;
int read() ;
int append() ;
int main()
{
string input ;
double cash ;
cout << "A simple program to enter information about clients.\n" ;
cout << "________________" << endl ;
cout << "If you wish to add a new client, press A. \n" ;
cout << "If you wish to see the list of existing clients, press B. \n" ;
cout << "If you wish to delete this file, press C. \n" ;
cout << "If you wish to delete a client, press D. \n" ;
cout << "________________" << endl ;
getline(cin, input ) ;
cout << endl ;
cout << endl ;
cout << endl ;
if(input == "A")
{
append() ;
}
if(input == "B")
{
read() ;
}
if(input == "C")
{
string input2;
cout << "Are you sure you wish to delete the entire file? You will lose the
information.\n"
cout <<"Enter C again, if you are sure: " ;
getline(cin, input2);
if(input2 == "C")
{
end();
}
}
if(input == "D")
{
kill();
}
return 0 ;
}
int kill()
{
string deleteline, nextline ;
string line;
ifstream kill;
kill.open("Clients.txt") ;
ofstream temp ;
temp.open("temp.txt") ;
cout << "What Client do you wish to delete from the database?\n" ;
getline(cin, deleteline) ;
deleteline = "Name: " + deleteline;
while (getline(kill,line))
{
if (line != deleteline )
{
temp << line << endl ;
}
}
temp.close();
kill.close();
remove("Clients.txt");
rename("temp.txt","Clients.txt");
}
int append()
{
string name ;
double cash ;
ofstream writer("Clients.txt" , ios::app) ;
cout << "Enter the client's name: " ;
getline(cin, name) ;
cout << "\n" ;
cout << "Enter the client's debt balance: " ;
cin >> cash ;
cout << endl ;
writer <<"Name: " << name << endl ;
writer <<"Debt: ";
writer << cash << "$" << endl ;
writer << "\n" ;
writer.close() ;
}
int read()
{
string line ;
ifstream read("Clients.txt") ;
if(read.is_open())
{
while(getline(read , line))
{
cout << line << "\n" ;
}
read.close() ;
}
}
int end()
{
ofstream ofs ;
ofs.open("Clients.txt", ios::out | ios::trunc);
ofs.close() ;
}
The main issue lies within, my kill() function, which is in charge of deleting the lines, and is called when the user enters "D" .
Solution
Read it without writing it:
while (getline(kill,line))
{
if (line != deleteline )
{
temp << line << endl ;
}
else
{
getline(kill, line); // Error handling is pretty irrelevant here.
}
}