Home  >  Article  >  Backend Development  >  How to input two variables in c++

How to input two variables in c++

下次还敢
下次还敢Original
2024-04-22 17:42:441016browse

Enter two variables in C: 1. Declare the variable; 2. Use cin to receive input; 3. Press Enter to end the input.

How to input two variables in c++

How to use C to input two variables

In C, you can use the following steps to input two variables:

1. Declare variables

First, you need to declare the types of these two variables, for example:

<code class="cpp">int num1, num2; // 声明两个整型变量</code>

2. Use cin to receive Input

Use the cin >> operator to receive the user's input from standard input and store it in a variable, for example:

<code class="cpp">cout << "请输入第一个数字:";
cin >> num1;
cout << "请输入第二个数字:";
cin >> num2;</code>

3. Press the Enter key to end the input

After the user enters two numbers, press the Enter key to end the input.

Example:

<code class="cpp">#include <iostream>

using namespace std;

int main() {
  int num1, num2;

  cout << "请输入第一个数字:";
  cin >> num1;
  cout << "请输入第二个数字:";
  cin >> num2;

  cout << "输入的两个数字是:" << num1 << " 和 " << num2 << endl;

  return 0;
}</code>

In this example, the program first declares two integer variables num1 and num2. Then, use cout to output a statement that prompts the user to enter a number. Use cin to receive two numbers entered by the user. Finally, use cout to output the two numbers entered.

The above is the detailed content of How to input two variables in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn