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

  • Thread starter Thread starter YoungPhysicist
  • Start date Start date
  • Tags Tags
    c++ input
AI Thread Summary
To read integers formatted as "a@b" in C++, it's recommended to use `cin` for input, but since it doesn't handle non-space-separated values well, it's better to read the input into a string first. This allows for flexible parsing using string methods or stringstream. For more complex formats, such as time values, string methods provide the necessary versatility. Regular expressions can also be utilized for advanced parsing, though they require the Boost library, which is a third-party addition rather than part of the standard library. Additionally, functions like `strtok()` can be considered for parsing strings. Overall, leveraging string manipulation techniques in C++ is essential for handling various input formats effectively.
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)?
 
Technology news on Phys.org
Last edited:
  • Like
Likes 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 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 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 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?
 
No I think boost is a third party library that you must download and install to use it.
 
  • Like
Likes YoungPhysicist
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 YoungPhysicist
Back
Top