4 Replies - 751 Views - Last Post: 25 October 2009 - 09:31 AM Rate Topic: -----

#1 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

Help me arrange my delete record code(working,just need to understand

Posted 24 October 2009 - 11:43 PM

Hello there, I have here a delete code wherein the code works exactly just like what I want, but I can't understand why it works like that. lol, it's just me who made that code though. here's how the code runs,

Quote

opens 2 file for reading and writing,
asks an account number,
then search the account number from clients.txt file
then copy the whole record inside clients.txt going to clients1.txt except for the account number that has been entered
delete clients.txt
rename clients1.txt to clients.txt



this work great though, but I want to add a message if the input account number is not found on the file
here's my code

 ofstream outClientFile("clients1.txt", ios::out);
 ifstream inClientFile("clients.txt", ios::in );



cout<<"Enter the Account Number you want to delete."<<endl;	
cin>>number;
								
	{
	while(inClientFile >> account >>fname>>mname>>lname >> balance )

	 if (account==number)			 //I think the problem starts here
				 cout<<" "<<endl;
	else							 
	   outClientFile<<account<< ' ' <<fname<< ' ' <<mname<<' '<<lname<<' '<<balance<<' '<<endl;
		cout<<"The record has been deleted"<<endl;
	}
	outClientFile.close();
	inClientFile.close();

	remove("clients.txt");
	rename("clients1.txt","clients.txt");




In my If statement, I want to add a message wherin if the entered account number does not exist in record inside clients.txt, a message will appear saying, "Account number does not exist." but can't figure out how to make it to work, cant someone help me plz..

also could you please help me make my code more clearer. ehe! B)

thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Help me arrange my delete record code(working,just need to understand

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Help me arrange my delete record code(working,just need to understand

Posted 25 October 2009 - 02:47 AM

Change your while to something like this:

account = -1;

while(inClientFile >> account >> fname >> mname >>lname >> balance)
{
	if (account==number)
	{
		cout << "The record is found and has been deleted" <<endl;
	}
	else			   
	{			  
		outClientFile << account << ' ' << fname << ' ' << mname << ' ' << lname << ' ' << balance << ' '<< endl;
	}
}

if (account == -1)
{
	cout << "The account was not found" << endl;
}


Also, notice where I positioned the curly braces, yours were a little off.

This post has been edited by Lillefix: 25 October 2009 - 02:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

Re: Help me arrange my delete record code(working,just need to understand

Posted 25 October 2009 - 09:05 AM

still now working, if I enter an account number which is not save in a txtfile, it is not going to show any message saying, "the account does not found".
Here's my updated delete code now.

ofstream outClientFile("clients1.txt", ios::out);
 ifstream inClientFile("clients.txt", ios::in );

 int account;  
	double balance;
	char fname[10];
	char mname[10];
	char lname[10];
	int number;

cout<<"Enter the Account Number you want to delete."<<endl;	
	   cin>>number;							
account = -1;

while(inClientFile >> account >> fname >> mname >>lname >> balance)
{
	if (account==number)
	{
		cout << "The record is found and has been deleted" <<endl;
	}
	else			   
	{			  
		outClientFile << account << ' ' << fname << ' ' << mname << ' ' << lname << ' ' << balance << ' '<< endl;
	}
}

if (account == -1)
{
	cout << "The account was not found" << endl;
}



	
	outClientFile.close();
	inClientFile.close();

	remove("clients.txt");
	rename("clients1.txt","clients.txt");




	void outputLine( int account, const string fname,const string mname, const string lname, double balance )
{
		cout << " "<< account << "  " << fname << " "<<mname<< " " <<lname
		<< "  " << setprecision(2) << right << balance << endl;
}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Help me arrange my delete record code(working,just need to understand

Posted 25 October 2009 - 09:14 AM

Spend a little time THINKING about the logic here, will you?

cout<<"Enter the Account Number you want to delete."<<endl;    
       cin>>number;                            
account = -1;

while(inClientFile >> account >> fname >> mname >>lname >> balance)
{
    if (account==number)
    {
        cout << "The record is found and has been deleted" <<endl;
    }
    else              
    {              
        outClientFile << account << ' ' << fname << ' ' << mname << ' ' << lname << ' ' << balance << ' '<< endl;
    }
}

if (account == -1)
{
    cout << "The account was not found" << endl;
}



Is account ever going to be -1 if anything is read in that while loop?
Was This Post Helpful? 0
  • +
  • -

#5 chubzyko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 08

Re: Help me arrange my delete record code(working,just need to understand

Posted 25 October 2009 - 09:31 AM

Nor. :D What I really want is, if the entered number is not found in the list of accounts, a message would show up saying that the account was not found. Hmn.. But i'm using while function, so it will check the first line in the txtfile if it has the same value, if not then it will do the "Else" statement then read again the 2nd line in the txtfile, again and again. :blink: I'm really confused. All I want is just a message. :ph34r:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1