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
Click For Summary
SUMMARY

To read integers in the format a@b using C++, the recommended approach is to first read the input into a string and then parse it. The C++ standard input method 'cin' is not suitable for directly handling non-space-separated inputs. Utilizing the stringstream class from the C++ Standard Library allows for flexible parsing of strings. Additionally, incorporating regular expressions via the Boost library can enhance string manipulation capabilities, particularly for complex formats.

PREREQUISITES
  • Understanding of C++ input/output operations
  • Familiarity with the stringstream class
  • Basic knowledge of string manipulation methods in C++
  • Experience with regular expressions and the Boost library
NEXT STEPS
  • Learn how to use stringstream for parsing strings in C++
  • Explore regular expressions with the Boost library for advanced string handling
  • Study string manipulation methods in C++ for better input processing
  • Investigate error handling techniques when parsing user input
USEFUL FOR

C++ developers, software engineers, and anyone looking to improve their skills in parsing complex input formats in C++ applications.

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

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
2
Views
2K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K