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

Discussion Overview

The discussion centers around reading two integers formatted as a@b in C++ using the cin input method. Participants explore various approaches to handle this input format, including string parsing and the use of libraries.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using scanf in C to read the integers directly from the specified format.
  • Another participant argues that cin is not suitable for reading inputs that are not space-separated and recommends reading the input into a string for parsing.
  • Some participants propose using string methods for flexibility in parsing various input formats, such as time values.
  • There is mention of using regular expressions and the Boost library for advanced string parsing techniques.
  • One participant questions whether the Boost library is part of the standard library, to which another participant responds that it is a third-party library requiring separate installation.
  • Another participant brings up the use of strtok as a potential method for parsing strings.

Areas of Agreement / Disagreement

Participants generally agree that using string methods for parsing is preferable, but there is no consensus on the best approach or the necessity of external libraries like Boost.

Contextual Notes

Some limitations include the assumption that participants are familiar with string parsing methods and the potential complexity of using third-party libraries.

Who May Find This Useful

Readers interested in C++ programming, particularly those dealing with input parsing and string manipulation techniques.

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   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?
 
No I think boost is a third party library that you must download and install to use it.
 
  • Like
Likes   Reactions: 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   Reactions: YoungPhysicist

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
5K
  • · 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 17 ·
Replies
17
Views
2K