Safi Ullah Ibne Sultan
Safi Ullah Ibne Sultan

Reputation: 622

Take input of two data without using space or enter

In C it is very simple scanf("%d : %d",&a,&b) say input 5:10 . So here a=5 and b=10. (:)Just split them into two as a separate integer. How can we do in C++ without using space or enter between two input

int a,b;
cin>>a>>b; // how we take input two integer taking as 5:10
cout<<a<<b; // a=5 and b=10

Upvotes: 7

Views: 97

Answers (2)

Rakete1111
Rakete1111

Reputation: 48948

If the delimiter is only 1 char long:

  • Calling std::cin.ignore()
  • Using a temporary char variable std::cin >> delimiter;

Those solutions should be put after getting the first number (std::cin >> a;) and before getting the second number (std::cin >> b;).

Upvotes: 0

Zereges
Zereges

Reputation: 5209

int main()
{
    int a, b;
    char c;
    std::cin >> a // Read first number,
             >> c // oh, there is a character I do not need
             >> b; // and read second
}

Or if you do not like having to declare that spare variable, this also works.

    std::cin >> a;
    std::cin.ignore(1);
    std::cin >> b;

Upvotes: 1

Related Questions