string - deleting ALL comment lines that start with // c++ -
string str; cout << "enter code\n"; getline(cin, str, '~'); //some loop can't figure out size_t nfpos = str.find('//'); size_t sec = str.find('\n', nfpos); size_t first = str.rfind('\n', nfpos); str.erase(first, sec - first); //end unknown loop
input
code
//comment
//comment
code~
output
code
//comment
code
i cannot life of me figure out kind of loop should utilize delete comments starting //. it's deleting first comment , bypassing else.
i've tried for, while, while, , if
i can't figure out
you should utilize
while(true) { size_t nfpos = str.find('//'); if(nfpos + 1) { size_t sec = str.find('\n', nfpos); size_t first = str.rfind('\n', nfpos); str.erase(first, sec - first); } else { break; } }
as mentioned here
while executing std::find()
, if no matches found, function returns string::npos
which defined static const size_t npos = -1;
so whenever match found it'll homecoming position of first character of first match, (so it'll non -1
).
if unable find match it'll homecoming -1
, else
section executed (because -1+1=0
, 0
equivalent false
), taking out of loop
c++ string loops comments erase
No comments:
Post a Comment