--- title: 關於 cin 的一些事情 tags: C++ --- # getline() 官方文件有說建議使用 `<string>` 函式庫的 `getline` 函式代替 cin 原因是 cin 不會檢查你丟了甚麼東西過來 但是又會把那個東西塞進去 **這樣會出問題** 所以要先用 getline 把東西接起來 ```cpp= #include <iostream> #include <string> using namespace std; int main(void){ //不要直接使用cin,先用getline去接你打的東西,然後去進行一些判斷 string mystr; cout << "What's your name? "; getline (cin, mystr); cout << "Hello " << mystr << ".\n"; cout << "What is your favorite movie? "; getline (cin, mystr); cout << "I like " << mystr << " too!\n"; return 0; }
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up