Reputation: 85
I am reading standard input, and want to stop skipping everything when I encounter a line that starts with an "a":
while(cin.peek() != 'a') {
cin.get();
}
The only problem is, this also triggers if I have an a in the middle of the line.
I would try cin.getline();
but I do not know the maximal length of the line. Is there a way to just go to the next line?
Upvotes: 6
Views: 4429
Reputation: 56
You could replace cin.get()
for cin.ignore(numeric_limits<streamsize>::max(), "\n")
.
Where numeric_limits<streamsize>::max()
is the maximum number of characters to be extracted and "\n"
indicates that the value to stop to extract characters is the End Of Line (in addition to End Of File).
Upvotes: 0
Reputation: 73366
You could do it yourself, like this:
#include <iostream>
#include <string>
int main() {
bool skip = true;
for(std::string line; std::getline(std::cin, line);)
{
if(line.size() && line[0] == 'a')
skip = false;
if(!skip)
std::cout << line<< std::endl;
}
return 0;
}
This will ignore every line read, until it encounters a non-empty line, starting with a
character. After that, it will stop skipping the lines read, and print every line.
Upvotes: 3