Вопрос

So I have a while look that checks every character in a file.

while (infile.get(ch))

The problem is that in the same loop I have to check the next character to check for some validation.

Is there anyway I could move the next character while keeping track of the current one?

Thanks

Это было полезно?

Решение

Use the peek() method so you will be able to look at the next character before extracting it. Here's an example:

std::istringstream iss("ABC");

for (char c; iss.get(c); )
{
    std::cout << "Current character is: " << c << std::endl;

    if (iss.peek() != EOF)
        std::cout << "Next character is: " << (char)iss.peek();
}

This should output:

 Current character is: A
 Next character is: B
 Current character is: B
 Next character is: C
 Current character is: C
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top