0

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" .

Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55
Bryan Fajardo
  • 161
  • 3
  • 15

1 Answers1

2

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.
   }
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • WOW, this works! I'm sorry man, but can you just please elaborate what you did here? I copied it and it works but I don't quite understand it. :( – Bryan Fajardo Dec 30 '13 at 07:16
  • Also, how would I go about deleting the spaces it leaves? So that it only leaves one space in between clients? – Bryan Fajardo Dec 30 '13 at 07:18
  • 1
    Basically when you use getline the file pointer iterates over the lines in the file successively. Hence by doing the above thing, the file pointer moves on to new line without writing the newline to the file and you next time it is pointing to the next client. – Uchia Itachi Dec 30 '13 at 07:19
  • 1
    @BryanFajardo: Do the same thing again in the `else` part to skip the space. – Uchia Itachi Dec 30 '13 at 07:25
  • Thank you very much man. I'm beginning to understand it better. :) – Bryan Fajardo Dec 30 '13 at 07:32