Receive numeric input based on c++ sin colon

Asked 2 years ago, Updated 2 years ago, 33 views

When you enter scanf in c language,

scanf("%d:%d", &a, &b);

You can only get numbers like this How do I write it using cin in c++?

c++ cin

2022-09-20 19:03

1 Answers

Assuming that you typed it correctly without exception handling, it is simple as below.

#include <iostream>

using namespace std;

int main()
{
    int a, b;
    char temp;
    cin >> a >> temp >> b;

    cout << a << '\n';
    cout << temp << '\n';
    cout << b << '\n';

    return 0;
}

However, handling inputs is one of the most troublesome exceptions. If you want to do the same thing as scanf even when the wrong value is entered, it is not as simple as above.

And it's good to think that C++ language is a language that includes C. If what you're trying to do is not in the C++ standard library, but it's in the C standard library, there's no reason not to use it. So you can use the scanf function.


2022-09-20 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.