tags: tgirc早修book

輸入與輸出

輸出

首先是最基本的輸出

#include <iostream> using namespace std; int main(){ cout<<"Hello World"<<endl; //輸出Hello World這個字串 //endl中的 l 是 L,不是數字 1 return 0; }

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

透過 cout (標準輸出)再搭配 << 這兩個指令就能印出想印出的訊息,文字要放在 "" 中,而後方的 endl 表示的是換行,也可用 "\n" 替代,在指令的最後請記得要附上 ; 表示結束(C++ 中每行指令後都要加上分號)

輸入

接下來是輸入

#include <iostream> using namespace std; int main(){ string s; //宣告字串s cin>>s; //將輸入內容存入s cout<<s<<"\n"; //輸出s的內容 return 0; }

先用 string 來宣告一個字串叫 s,再透過 cin>> 輸入想列印出來的文字,直到遇到換行或是空白時停止,接著透過剛剛介紹的輸出,列印輸入的文字以及換行