简体   繁体   English

无法在txt文件中的getline(cin,(variable))之后删除nextline

[英]Can't delete nextline after getline(cin , (variable)) in a txt file

I am writing a simple program where the user can: 我正在编写一个简单的程序,用户可以:

A: add a new client 答:添加一个新客户端
B: see list of clients B:查看客户列表
C: delete the database C:删除数据库
D: delete a client D:删除客户端

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. 我只用了大约1个月的时间进入c ++所以如果可以的话请为我愚蠢地理解我会非常感激。 :) :)

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" . 主要问题在于我的kill()函数,它负责删除行,并在用户输入“D”时调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM