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

  • Thread starter YoungPhysicist
  • Start date
  • Tags
    c++ input
In summary, when working with C++ and wanting to read two integers in the format of a@b, it is not recommended to use cin. Instead, it is suggested to read the input into a string and use string methods to parse it. Alternatively, one can use a third party library like Boost to handle more complex string formats. Regular expressions can also be useful for parsing.
  • #1
YoungPhysicist
Insights Author
350
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
  • #2
Last edited:
  • Like
Likes harborsparrow, QuantumQuest and YoungPhysicist
  • #3
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
  • #4
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
  • #5
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
  • #6
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?
 
  • #7
No I think boost is a third party library that you must download and install to use it.
 
  • Like
Likes YoungPhysicist
  • #8
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

What is cin in C++?

cin is an input stream object in C++ that is used to read data from the user's keyboard or other input devices. It is part of the iostream library and is commonly used for user input in console applications.

How do I use cin to read integers in C++?

To use cin to read integers, you can declare an integer variable and use the extraction operator (>>) to store the user's input into the variable. For example, "int num; cin >> num;" will store the user's input into the variable "num".

What is the "@" symbol used for when reading integers with cin?

The "@" symbol is not a special symbol when using cin to read integers. It can be used as a delimiter to separate two integers being read in a single line. For example, if the user enters "5@10" when prompted, cin will store 5 in the first integer variable and 10 in the second integer variable.

Can cin read multiple integers in a single line?

Yes, cin can read multiple integers in a single line. To do so, the integers must be separated by whitespace or a delimiter, such as the "@" symbol. You can use a loop to read multiple integers until the end of the line is reached.

What happens if the user enters a non-integer value when using cin to read integers?

If the user enters a non-integer value when using cin to read integers, the input will be ignored and the program will continue to execute. It is important to handle invalid input by checking for errors and prompting the user to re-enter a valid input.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
881
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
2
Views
934
Replies
10
Views
961
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top