C++ Can';t删除txt文件中getline(cin,(变量))之后的下一行

C++ Can';t删除txt文件中getline(cin,(变量))之后的下一行,c++,function,ifstream,getline,ofstream,C++,Function,Ifstream,Getline,Ofstream,我正在编写一个简单的程序,用户可以: A:添加新客户端 B:见客户名单 C:删除数据库 D:删除客户端 它在大多数情况下运行良好,问题基本上是每个客户都有债务。 债务显示在客户名称下方的行中 Name: bryan Debt: 45.56$ Name: Rosa Debt: 23.43$ Name: Jon Debt: 55.55$ 我想这样做,当客户的名字被删除时,它下面的那一行也会被删除,等等。什么代码可以让它工作?我只在C++中呆了1个月,所以如果你能把它哑掉让我理解,我会非常感激。

我正在编写一个简单的程序,用户可以:

A:添加新客户端
B:见客户名单
C:删除数据库
D:删除客户端

它在大多数情况下运行良好,问题基本上是每个客户都有债务。 债务显示在客户名称下方的行中

Name: bryan
Debt: 45.56$

Name: Rosa
Debt: 23.43$

Name: Jon
Debt: 55.55$
我想这样做,当客户的名字被删除时,它下面的那一行也会被删除,等等。什么代码可以让它工作?我只在C++中呆了1个月,所以如果你能把它哑掉让我理解,我会非常感激。p> 我的代码:

#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() ;
}
#包括
#包括
#包括
使用名称空间std;
int kill();
int end();
int read();
int append();
int main()
{
字符串输入;
双倍现金;

不能读而不写:

while (getline(kill,line))
{
   if (line != deleteline )
   {
      temp << line << endl ;            
   }
   else
   {
      getline(kill, line);   // Error handling is pretty irrelevant here.
   }
}
while(getline(kill,line))
{
如果(行!=删除行)
{

哇,这很管用!对不起,伙计,你能详细说明一下你在这里做了什么吗?我复制了它,它很管用,但我不太明白(还有,我该如何删除它留下的空格?这样它在客户端之间只留下一个空格?基本上,当您使用getline时,文件指针会依次在文件中的行上迭代。因此,通过执行上述操作,文件指针会移到新行,而不会将新行写入文件,下次它是pointi时,您将看到它。)ng转到下一个客户。@BryanFajardo:在
的else
部分再次做同样的事情,跳过空格。非常感谢你,伙计。我开始更好地理解它了。:)