Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีอ่านและพิมพ์ค่าจำนวนเต็ม (Integer) ใน C++

คำอธิบาย

ที่นี่เราจะดูวิธีอ่านจำนวนเต็มจากผู้ใช้และแสดงผลใน C++ เพื่อรับนำเข้า เราจะใช้ cin operator และเพื่อแสดงผลเราจะใช้ cout operator รูปแบบจะเป็นดังนี้:

นำเข้า (Input):

int x; cin >> x;

ผลลัพธ์ (Output):

int x = 110; cout << x;

ตัวอย่างโค้ด

#include<iostream> using namespace std; int main(int argc, char const *argv[]) {    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; }

ผลลัพธ์

Enter some value: 100 The given value is: 100 The value of y is: 50