Using cin to Read Integers in Formation a@b in C++

  • Context: C/C++ 
  • Thread starter Thread starter YoungPhysicist
  • Start date Start date
  • Tags Tags
    c++ input
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
YoungPhysicist
Insights Author
Messages
350
Reaction score
203
if I want to read to integer a and b and they are in a formation of a@b, I CAN SIMPLY USE

scanf(%d@%d,&a,&b)

to read the input. But how should I do that in c++(with cin)?
 
Physics news on Phys.org
Last edited:
  • Like
Likes   Reactions: harborsparrow, QuantumQuest and YoungPhysicist
I would get familiar with string methods and read the input into a string that you can then parse anyway you want.

As an example, you might want to input time values allowing either a decimal format or the hour:minute:second format. String methods would give you the flexibility you’d need to parse the input.
 
  • Like
Likes   Reactions: FactChecker
jedishrfu said:
As an example, you might want to input time values allowing either a decimal format or the hour:minute:second format. String methods would give you the flexibility you’d need to parse the input.
Agree. Especially since one can try parsing one way, test for an error, and parse another way if necessary.
 
  • Like
Likes   Reactions: jedishrfu
Here's a tutorial on string methods in C++:

https://cal-linux.com/tutorials/strings.html

It mentions regular expressions and using the Boost library too. Regular expressions can dice up some pretty fancy string formats and is a useful skill for using GREP, SED or AWK commands in shell scripts (or not) under Unix / MacOSX / Linux.
 
  • Like
Likes   Reactions: YoungPhysicist, QuantumQuest and FactChecker
jedishrfu said:
Here's a tutorial on string methods in C++:

https://cal-linux.com/tutorials/strings.html

It mentions regular expressions and using the Boost library too. Regular expressions can dice up some pretty fancy string formats and is a useful skill for using GREP, SED or AWK commands in shell scripts (or not) under Unix / MacOSX / Linux.
Is the <boost/regex.hpp> it mentioned in the standard library?
 
jedishrfu said:
As an example, you might want to input time values allowing either a decimal format or the hour:minute:second format. String methods would give you the flexibility you’d need to parse the input.
strtok() comes to mind...
 
  • Like
Likes   Reactions: YoungPhysicist