第一章 C++編程基礎(如何撰寫C++程式)__Linux

來源:互聯網
上載者:User

P1.1  p6

 

讀取使用者姓名並儲存,最後向使用者打招呼。

 

#include<iostream>  // 1

#include<string>   //2

using namespace std;  //3

int main()

{

   string user_name;

   cout<<"please enter your first name:";

   cin>> user_name;

   cout<<'\n'

   cout<<"hello,"

      << user_name

      <<"welcome,and bye bye...";

 

return o;      

 

}

 

1.c++標準的的“輸入輸出庫”名為iostream,其中包含相關整套的class,用於支援對終端和檔案的輸入與輸出。

2.string class 用於儲存資料。

3.using namespace std

4.字元常量分為兩類:一類為可列印字元,如(英文,數字,標點符號),另一種為不可列印字元,如(分行符號\n  定位字元\t...)字元常量由一組單引號括住。

5.cin,cout的用法見代碼。

 

 

習題1.4  在上題擴充,要求分別輸入使用者的姓和名,並列印使用者的姓和名。

 

#include<iostream>

#include<string>

using namespace std;

int main()

 

{

   string user_first name,user_last name;

   cout<<”hello,please enter your first name”;

   cin>>user_first name;

   cout<<”hello,please enter your last name”;

   cin>>user_last name;

   cout<<”hello\n’

       <<user_first name

       <<’ ‘

       <<user_last name;

 

       Return 0;

 

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.