Hi Everyone,
I wrote a program for Linux which goes through a file called List.txt, finds words with the letters M and T in them and then pastes these words in another file called MandTwords.txt. The source code is as follows:
list.cpp: In function ‘int main()’:
list.cpp:19: error: request for member ‘find’ in ‘list_item’, which is of non-class type ‘char [10]’
list.cpp:20: error: request for member ‘find’ in ‘list_item’, which is of non-class type ‘char [10]’
Could someone please help me out with this?
Thanks alot!
list.cpp: In function ‘int main()’:
list.cpp:19: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)
Well, I did not realize you had a getline() method in there. Just assign the array in getline() to a string and then use that string's find method. I have provided the code below for your reference.
ifstream iFile;
string item_list;
//If you want to read until you find a '\n'
getline(iFile, item_list);
//if you want to read per word
iFile >> item_list;
Firstly, just want to thank everyone for their help.
The program does compile now - but there's a runtime error. It does not proceed beyong the cin.getline() part. It just halts there. Pressing enter repeatedly forces the loop to proceed and then it just goes on till infinity.
The code is as written by naivnomore above.
ne555 is absolutely correct. In your code, you are reading the lines from cin which refers to the console but your condition in the while loop checks if your iFile is good. That is why it loops for over. Please change your getline code so that it gets invoked on the iFile object instead of the cin object.
Thanks guys - your comments made a lot of sense. It seems silly to direct getline to cin instead of the string. I fixed that and now the code does actually go right to the end.
Here's what it looks like now: