<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>&#91;C++&#93;. Nhập Xuất Với Cin và Cout</title>
<meta name="description" content="&#91;C++&#93;. Nhập Xuất Với Cin và Cout - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;c-nhap-xuat-voi-cin-va-cout-445.html">
<meta name="author" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta name="copyright" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG [webmaster@tinhocdct.xyz]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="&#91;C++&#93;. Nhập Xuất Với Cin và Cout">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;c-nhap-xuat-voi-cin-va-cout-445.html">
<meta property="og:site_name" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta property="og:url" content="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-nhap-xuat-voi-cin-va-cout-445.html">
<link rel="shortcut icon" href="https://tinhocdct.xyz/favicon.ico">
<link rel="canonical" href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-nhap-xuat-voi-cin-va-cout-445.html">
<link rel="alternate" href="https://tinhocdct.xyz/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-tuc/" title="Tin Tức - TIN TỨC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thong-cao-bao-chi/" title="Tin Tức - THÔNG BÁO" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ban-tin-noi-bo/" title="Tin Tức - BẢN TIN NỘI BỘ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-cong-nghe/" title="Tin Tức - CÔNG NGHỆ - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thu-thuat/" title="Tin Tức - THỦ THUẬT" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tien-ich/" title="Tin Tức - TIỆN ÍCH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ai/" title="Tin Tức - AI" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/khoa-hoc/" title="Tin Tức - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-chuyen-mon/" title="Tin Tức - HOẠT ĐỘNG CHUYÊN MÔN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-cong-doan/" title="Tin Tức - CÔNG ĐOÀN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-doan-the/" title="Tin Tức - ĐOÀN THỂ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-huong-nghiep/" title="Tin Tức - TUYỂN SINH - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-2025/" title="Tin Tức - TUYỂN SINH 2025" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/lich-cong-tac-tuan/" title="Tin Tức - Lịch công tác tuần" type="application/rss+xml">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/language/vi.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/global.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/news.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/main.js">
<link rel="preload" as="script" href="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js">
<link rel="StyleSheet" href="https://tinhocdct.xyz/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/bootstrap.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/news.css">
<style>
	body{background: #fff;}
</style>
	</head>
	<body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG</h2>
		<p class="pull-right"><a title="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG" href="https://tinhocdct.xyz/">https://tinhocdct.xyz</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>&#91;C++&#93;. Nhập Xuất Với Cin và Cout</h1>
		<ul class="list-inline">
			<li>Thứ sáu - 21/02/2025 03:26</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			Thao tác nhập dữ liệu từ bàn phím và hiển thị dữ liệu ra màn hình là thao tác đầu tiên bạn cần thành thạo khi học bất cứ ngôn ngữ lập trình nào. Bài viết này mình sẽ hướng dẫn các bạn nhập xuất một cách thành thạo với cin và cout
		</div>
				<div class="imghome">
			<img alt="nhập xuất dữ liệu trong c++" src="https://tinhocdct.xyz/uploads/news/2025_02/nhap-xuat-du-lieu-trong-c.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="font-size: 14pt; color: rgb(53, 152, 219);" style="font-size:14pt"><span style="box-sizing:border-box"><span style="color:rgb(53, 152, 219);"><strong style="box-sizing:border-box; font-weight:bolder">1. In Ra Màn Hình Với Cout</strong></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">cout&nbsp;</span></span>là một đối tượng giúp bạn có thể hiện thị nội dung như : số nguyên, số thực, giá trị của biến, đoạn text ra màn hình. Để sử dụng đối tượng này bạn cần khai báo thư viện&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&quot;iostream&quot;</span></span>&nbsp;và sử dụng namespace&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">std</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">cout được sử dụng đi kèm với toán tử chèn&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&lt;&lt;&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
&nbsp;
<pre>
<code class="language-cpp">cout &lt;&lt; &quot;hello 28tech&quot; &lt;&lt; endl; // Hiển thị dòng hello 28tech lên màn hình và xuống dòng
cout &lt;&lt; 100 &lt;&lt; endl; // Hiển thị số 100 lên màn hình và xuống dòng
cout &lt;&lt; banKinh &lt;&lt; endl; // Hiển thị giá trị của biến banKinh lên màn hình</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">endl&nbsp;</span></span>là một đối tượng đặc biệt giúp bạn có thể xuống dòng khi sử dụng cout, bạn sẽ thêm đối tượng này trong câu lệnh cout tại vị trí bạn muốn xuống dòng.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">Ví dụ 1</span></span>&nbsp;:&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<pre>
<code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main(){
    cout &lt;&lt; &quot;28tech blog&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;28tech.com.vn&quot; &lt;&lt; &quot; &quot; &lt;&lt; &quot;C++ programming !&quot; &lt;&lt; endl;
    cout &lt;&lt; 100 &lt;&lt; &quot; &quot; &lt;&lt; 200 &lt;&lt; &quot; &quot; &lt;&lt; 300 &lt;&lt; endl;
    int n = 282828;
    cout &lt;&lt; &quot;Gia tri cua bien n : &quot; &lt;&lt; n &lt;&lt; endl;
    
    return 0;
}</code></pre>
<br  />
Output :&nbsp;<br  />
&nbsp;
<pre>
<code>28tech blog
28tech.com.vn C++ programming !
100 200 300
Gia tri cua bien n : 282828</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">Ví dụ 2&nbsp;</span></span>: Khi in ra giá trị của kiểu bool, nếu giá trị true sẽ được hiển thị số 1, ngược lại giá trị false sẽ được hiển thị số 0</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Nếu bạn muốn hiển thị giá trị true, false bạn thêm cụm&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">boolalpha&nbsp;</span></span>trước biến bool này.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<pre>
<code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main(){
    bool check = true;
    bool used = false;
    cout &lt;&lt; check &lt;&lt; &quot; &quot; &lt;&lt; used &lt;&lt; endl;
    cout &lt;&lt; boolalpha &lt;&lt; check &lt;&lt; endl;
    cout &lt;&lt; boolalpha &lt;&lt; used &lt;&lt; endl;
    return 0;
}</code></pre>
<br  />
Output :&nbsp;<br  />
&nbsp;
<pre>
<code>1 0
true
false</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(53, 152, 219);" style="box-sizing:border-box"><span style="color:rgb(53, 152, 219);"><strong style="box-sizing:border-box; font-weight:bolder">Lưu ý khi in giá trị của số thực float, double</strong></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Đối với kiểu dữ liệu số thực, đề bài thường yêu cầu bạn in ra các số này với độ chính xác nhất định, cụ thể là bao nhiêu chữ số phần thập phân</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Để in ra giá trị số float, double với độ chính xác k chữ số sau dấu phẩy bạn thêm cụm&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&lt;&lt; fixed &lt;&lt; setprecision(k)</span></span>&nbsp;trước tên biến.&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Thư viện chứa đối tượng setprecision là thư viện&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&quot;iomanip&quot;</span></span>, bạn cần thêm vào trước khi sử dụng.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">Ví dụ 3 :&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<pre>
<code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

int main(){
    float a = 30.192387;
    double b = 9.18293;
    cout &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; fixed &lt;&lt; setprecision(10) &lt;&lt; b &lt;&lt; endl;
    return 0;
}</code></pre>
<br  />
Output :&nbsp;<br  />
&nbsp;
<pre>
<code>30.19
9.1829300000</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(53, 152, 219);" style="box-sizing:border-box"><span style="color:rgb(53, 152, 219);"><strong style="box-sizing:border-box; font-weight:bolder">Xác định độ rộng khi in với setw và setfill</strong></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Khi bạn muốn in ra giá trị của 1 số, 1 biến hay đoạn text với độ rộng tương đương bao nhiêu dấu cách bạn có thể sử dụng&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">setw(k)</span></span>&nbsp;với k là độ rộng trước tên biến hay nội dung bạn muốn in.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Trong trường hợp bạn muốn thay thế các dấu cách khi nội dung bạn in ra không đủ độ rộng chỉ định bạn có thể sử dụng&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">setfill(c)</span></span>&nbsp;với c là ký tự bạn muốn thay thế cho các dấu cách trống.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">Ví dụ 4:&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<pre>
<code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

int main(){
    int n = 12345;
    cout &lt;&lt; setw(10) &lt;&lt; n &lt;&lt; endl;
    cout &lt;&lt; setw(10) &lt;&lt; setfill(&#039;0&#039;) &lt;&lt; n &lt;&lt; endl;
    cout &lt;&lt; setw(10) &lt;&lt; setfill(&#039;#&#039;) &lt;&lt; &quot;28tech&quot; &lt;&lt; endl;
    return 0;
}</code></pre>
<br  />
Output :&nbsp;<br  />
&nbsp; &nbsp;
<pre>
<code>12345
0000012345
####28tech</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="font-size: 14pt; color: rgb(53, 152, 219);" style="font-size:14pt"><span style="box-sizing:border-box"><span style="color:rgb(53, 152, 219);"><strong style="box-sizing:border-box; font-weight:bolder">2. Nhập Từ Bàn Phím Với Cin</strong></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">cin&nbsp;</span></span>là đối tượng giúp bạn có thể nhập dữ liệu từ bàn phím thay vì khởi tạo thủ công, cin nằm trong thư viện&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&quot;iostream&quot;</span></span>&nbsp;và namespace&nbsp;<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">std</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">cin</span></span>&nbsp;đi kèm với toán tử nhập<span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">&nbsp;&gt;&gt;&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
&nbsp;
<pre>
<code class="language-cpp">cin &gt;&gt; a; // Nhập giá trị cho a từ bàn phím
cin &gt;&gt; x &gt;&gt; y &gt;&gt; z; // Nhập giá trị cho 3 biến x, y, z lần lượt từ bàn phím</code></pre>
<br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Khi nhập dữ liệu từ bàn phím bạn sẽ nhập dữ liệu xong sau đó enter, nếu bạn nhập nhiều số cùng 1 lúc thì cần có dấu cách giữa các số sau đó ấn enter sau số cuối cùng</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial">Ví dụ trong câu lệnh thứ 2 ở trên khi nhập a, b, c thì bạn có thể nhập từng số rồi enter hoặc nhập 3 số cách nhau 1 dấu cách rồi mới enter cũng được.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br  />
<span style="font-size:15px"><span style="box-sizing:border-box"><span style="color:rgb(82, 87, 92);"><span style="font-family:&#039;Be Vietnam Pro&#039;, sans-serif"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-variant-caps:normal"><span style="font-weight:400"><span style="letter-spacing:normal"><span style="orphans:2"><span style="text-transform:none"><span style="widows:2"><span style="word-spacing:0px"><span style="white-space:normal"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span data-mce-style="color: rgb(230, 126, 35);" style="box-sizing:border-box"><span style="color:rgb(230, 126, 35);">Ví dụ 1 :&nbsp;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<pre>
<code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

int main(){
    int n, a, b, c;
    cout &lt;&lt; &quot;Nhap gia tri cho n : &quot;;
    cin &gt;&gt; n;
    cout &lt;&lt; &quot;Gia tri n vua nhap la : &quot; &lt;&lt; n &lt;&lt; endl;
    cout &lt;&lt; &quot;Nhap gia tri cho a, b, c : &quot;;
    cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;
    cout &lt;&lt; &quot;Gia tri a, b, c : &quot; &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt; b &lt;&lt; &quot; &quot; &lt;&lt; c &lt;&lt; endl;
    return 0;
}</code></pre>
&nbsp;

<div><a href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-chu-thich-comment-444.html" target="_blank"><img alt="bai truoc" height="35" src="https://tinhocdct.xyz/uploads/news/2025_02/bai-truoc.png" width="120" /></a>&nbsp;<img alt="bai tiep" height="35" src="https://tinhocdct.xyz/uploads/news/2025_02/bai-tiep.png" style="float:right" width="120" /></div>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả bài viết:</strong>
				Thanh Sơn
			</p>
						<p>
				<strong>Nguồn tin:</strong>
				blog.28tech.com.vn
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-nhap-xuat-voi-cin-va-cout-445.html" title="&#91;C++&#93;. Nhập Xuất Với Cin và Cout">https://tinhocdct.xyz/hoat-dong-chuyen-mon/c-nhap-xuat-voi-cin-va-cout-445.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG
		</div>
		<div id="contact">
			<a href="mailto:webmaster@tinhocdct.xyz">webmaster@tinhocdct.xyz</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://tinhocdct.xyz/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="" src="/index.php?second=cronjobs&amp;p=y4vynAde" width="1" height="1" /></div>
<script src="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_is_recaptcha=0;</script>
<script src="https://tinhocdct.xyz/assets/js/language/vi.js"></script>
<script src="https://tinhocdct.xyz/assets/js/global.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/news.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/main.js"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198"
     crossorigin="anonymous"></script>
<script src="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js"></script>
</body>
</html>