<style> p { font-size: 20px; font-weight: bold; } li{ font-size: 20px; } </style> <h1> c++ 連續輸入並讀取整數部分 </h1> <p> 輸入a b兩數為一組,最多輸入10組,可提前結束輸入 </p>  <p> 使用一次回車來結束輸入 </p> <p> 這次程式使用 <li>string.empty()</li> <li>sscanf_s(string, "%d %d", &int a, &int b)</li> </p> <p> string.empty() </p> <p style="font-size: 18px;"> 判斷string字串裡面是否為空 空則回傳true 反之false </p> <p> sscanf_s(string, "%d %d", &int a, &int b) </p> <p style="font-size: 18px;"> 從一個字串當中按照指定格式解析並讀取至指定的變數 </p> <p style="font-size: 17px; color :red;">ps:c++應在string加上.c_str才能實現與c的互動</p> <p> 實例寫法: </p> <pre><code style="font-size: 20px;">bool end = false; while (k < 10 && !end) { string ch; getline(cin, ch); if (ch.empty()) { ed++; } else { k = 0; if (sscanf_s(ch.c_str(), "%d %d", &ar, &br) == 2) { a[i++] = ar; b[j++] = br; } } if (ed == 1) { end = true; } } </code></pre>
×
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