问题
i have spent hours trying to get this to work and have been all over your website and nothing is working for me.....
basically i am working on a banking system for an assignment and this is the final thing that i need to do in order to complete my unit.
i am trying to read multiple lines from within a text file so i can output them. these will be my previous transactions.
i have managed to get it to store my values but i cannot, for the life of me, read the lines due to the fact that the "getline" code doesnt work. here is my code
static int linecount = 0 ;
ifstream readTransactions;
readTransactions.open("deposit");
string line ;
if ( readTransactions )
{
while (getline (readTransactions , line ) ) {
cout<<linecount<<": "<<line<<'\n';//supposing '\n' to be line end
linecount++ ;
}
readTransactions.close( ) ;
return 0 ;
回答1:
You probably forgot to include the string
header:
#include <string>
回答2:
if ( readTransactions ) has no problem. I want to know if 'reading multiple lines' means you see multiple lines are outputing on screen once. If yes, try endl instead of '\n'.
来源:https://stackoverflow.com/questions/21727206/getline-undefined-reading-multiple-lines