The first scanf in the code below matches and discards the next input stream character while the second scanf matches and discards the next input stream character if it is a colon. Is there any way to mimic this behavior using cin>> other than manually inspecting the value of the character it stores in variable ch?
scanf("%*c");
scanf(":");
char ch;
cin >> ch;
for single characters it is often easier to use get/peek/unget rather than >>
.
cin.get(); // read a single character (discarding it)
if (cin.peek() == ':') cin.get(); // discard a ':'
if (cin.get() != ':') cin.unget(); // same thing
However, iostreams have no real built in "scanning" like scanf.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.