Could not convert 'line.std...' to bool

Hi guys,

I'm quite new to C++ programming.
I was trying to write a simple c++ to delete line from a text file but got some errors.
Any help will be appreciated.

This is the line:

int Transaction::delexpense()
{
string expitem, line;
cout << "Enter the expense you wish to delete:" << endl;
cin >> expitem;
ifstream in ("transaction.txt");
if (!in.is_open())
{
cout << "Record does not exists" << endl;
}
ofstream out ("temp.txt");
while (getline(in, line))
{
if ((line = line.find(expitem)))
out << line << "\n";
}
in.close();
out.close();
remove ("transaction.txt");
rename ("temp.txt", "transaction.txt");
}


But then I got the below error:

Transaction.cpp:74:40: error: could not convert ‘line.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>](((int)((char)line.std::basic_string<_CharT, _Traits, _Alloc>::find [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>](((const std::basic_string<char>&)((const std::basic_string<char>*)(& expitem))), 0ul))))’ to ‘bool’


Thanks!
Error messages can be like that when there are templates involved...

The problem seems to be line = line.find(expitem) line is a string, and find returns an integer (size_t). You can't assign one to another.
Hi hamsterman,

I also found similar explanation over the Internet.
What I'm trying to do here is to delete the line input by user. So it is a string right?
How should I write it?

Thanks!
If you want to delete the line that contains string user entered somewhere in it, then write
if( line.find( expitem ) == string::npos ) out << line << "\n";
.find returns the position of the found string or string::npos if it was not found.

If you want to delete a line that matches user input exactly, replace that condition with
line != expitem
Hi hamsterman,

Thanks!
That seems to have worked.
I will continue to work on it!
Topic archived. No new replies allowed.