c++ multiple inputs
Here is an example of how to handle multiple inputs in C++:
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
// Prompt the user to enter the first number
cout << "Enter the first number: ";
cin >> num1;
// Prompt the user to enter the second number
cout << "Enter the second number: ";
cin >> num2;
// Calculate the sum
sum = num1 + num2;
// Display the result
cout << "The sum is: " << sum << endl;
return 0;
}
Explanation:
We include the
iostream
library, which allows us to use input/output operations in C++.We declare three integer variables:
num1
,num2
, andsum
. These variables will store the input values and the sum of the two numbers, respectively.The
cout
statement is used to prompt the user to enter the first number.The
cin
statement is used to read the input value from the user and store it in thenum1
variable.Similarly, we prompt the user to enter the second number using another
cout
statement, and read the input value using acin
statement, storing it in thenum2
variable.We calculate the sum of
num1
andnum2
and store it in thesum
variable.Finally, we use a
cout
statement to display the result to the user.
Note: It is important to include the line using namespace std;
at the beginning of the program to avoid having to specify std::
before each input/output operation.