C++ multiple input in a single line
To take multiple inputs in a single line in C++, you can use the cin
object from the iostream
library along with the extraction operator >>
. The extraction operator is used to extract values from the input stream and store them in variables.
Here are the steps to take multiple inputs in a single line in C++:
Include the
iostream
library:cpp #include <iostream> using namespace std;
Declare variables to store the inputs:
cpp int a, b, c;
Use the
cin
object and extraction operator>>
to take inputs:cpp cin >> a >> b >> c;
In this step, you can separate the inputs by space or any other delimiter. The extraction operator >>
will read the input value and assign it to the corresponding variable.
- Process the inputs as needed.
Here is an example to illustrate the usage:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three integers: ";
cin >> a >> b >> c;
// Process the inputs
int sum = a + b + c;
cout << "Sum of the three numbers: " << sum << endl;
return 0;
}
In this example, the user is prompted to enter three integers. The inputs are then read using cin
and the sum of the three numbers is calculated and displayed.
I hope this explanation helps! Let me know if you have any further questions.