dimanche 1 septembre 2019

Problem Using cin.get with "while" statement instead of using "if" statement in c++

I am new in c++ . and I am practicing some new code to use the character as a variable . in the book it is using while loop to use cin but I can't get the idea behind it. thos code takes all inputted characters and then ignore the 'b' char.

char a;
while (cin.get(a)) {
    cout<<a;
    while(cin.peek()=='b')
         cin.ignore(1,'b');
}

  1. In this loop, it is taking a character every time it executes the loop. so at first run, it takes for example "a" then the code says : while(cin.peek()=='a') cin.ignore(1,'b') what is the meaning of while (cin.peek=='a') continue the loop. so isn't it more reasonable to write: if (con.peek='a')? it doesn't seem to be a loop it is just one statement that tells if u get the char then ignore it. why they use the code with a while loop?

Aucun commentaire:

Enregistrer un commentaire