Hướng dẫn đọc số nguyên từ người dùng và hiển thị ra màn hình trong C++.
Cú pháp
- Đầu vào:
int x; cin >> x; - Đầu ra:
int x = 110; cout << x;
Chương trình ví dụ
#include <iostream>
using namespace std;
int main() {
int x;
int y = 50;
cout << "Enter some value: ";
cin >> x;
cout << "The given value is: " << x << endl;
cout << "The value of y is: " << y;
}
Kết quả mẫu
Enter some value: 100 The given value is: 100 The value of y is: 50
Giải thích
cin(console input) dùng toán tử>>(extraction operator)cout(console output) dùng toán tử<<(insertion operator)endlxuống dòng và flush buffer- Có thể chain:
cin >> a >> b >> c;