2 Replies - 181 Views - Last Post: 08 September 2012 - 07:10 AM Rate Topic: -----

#1 glenn16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-September 12

error:

Posted 08 September 2012 - 01:29 AM

inside the infile.txt:
Adobo
Carbohydrates
Meal
Philippines
Very_good
9

i need to delete all 6 info in the text file. but my code can only delete one line.

    	char deleteline[50], a[20];
	string line;
	ifstream sup;
    sup.open("infile.txt");
    ofstream temp;
    temp.open("myfile.txt");
	information lists;
    cout << "What INFORMATION do you want to remove? ";
    cin >> deleteline;
	

	
    while (getline(sup, line))
    {
		for (int x = 1; deleteline[x] != '\0'; x++)
	{
        if (line != deleteline)
		{
			temp << line << endl;
			cout << "Information(s) was succesfully deleted." << endl;
						
		}
       	else 
			cout << "Information(s) was not found." << endl;
		
    }
	}
	

    temp.close();
    sup.close();
    remove("infile.txt");
    rename("myfile.txt","infile.txt");


Is This A Good Question/Topic? 0
  • +

Replies To: error:

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: error:

Posted 08 September 2012 - 06:12 AM

Your code only compares against the first item in deleteline.

You're comparing a C string (deleteline) to a C++ string (line). Use one or the other.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • Joined: 05-May 12

Re: error:

Posted 08 September 2012 - 07:10 AM

Also be careful with your for loop condition on line 15. If the very first line you read from the file is a blank line, and you are unlucky enough that the random data in the deleteline array does not contain any zeroes, then you will quickly read data beyond the array range.

I don't think you even need that for loop, but you may have your own reasons for having it. It's just not obvious why, though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1