Logo
Section 1 / 2

用 while 循环处理持续输入

通过构建一个每日步数追踪器来学习 C++ 的 while 循环,该程序会持续询问用户输入步数,直到用户完成为止。

导入

用 while 循环处理持续输入

通过构建一个每日步数追踪器来学习 C++ 的 while 循环,该程序会持续询问用户输入步数,直到用户完成为止。

...
1#include <iostream> 2using namespace std; 3 4int main() { 5 int count = 1; 6 7 while (count <= 5) { 8 cout << "This is loop number " << count << endl; 9 count = count + 1; 10 } 11 12 return 0; 13}

No configuration found for ID: 85eccdace83c845028b7a69baa41afe297825f9c3573ec9a063a5ff9f73f128d

代码示例

1Enter your steps (or type 'done' to finish): 3000 2Enter your steps (or type 'done' to finish): 4500 3Enter your steps (or type 'done' to finish): 2200 4Enter your steps (or type 'done' to finish): done 5 6Total steps: 9700 7Great job staying active!
PageFlux AI 助手

👋 你好!我是 PageFlux AI 助手,专门帮助您理解课程内容。有什么问题尽管问我吧!

💡 选中文本后点击"AI 询问",或 Ctrl+Enter 发送消息

No configuration found for ID: 7eef46cf1588968fdc4a76ce40545b13d3f28b06c99b9a31fd5af967f5caf529

1#include <iostream> 2#include <string> 3using namespace std; 4 5int main() { 6 int total_steps = 0; 7 string user_input; 8 9 cout << "Enter your steps (or type 'done' to finish): "; 10 getline(cin, user_input); 11 12 while (user_input != "done") { 13 bool is_number = true; 14 15 // Validate input 16 for (char c : user_input) { 17 if (!isdigit(c)) { 18 is_number = false; 19 break; 20 } 21 } 22 23 if (is_number) { 24 int steps = stoi(user_input); 25 total_steps += steps; 26 } else { 27 cout << "Invalid input. Please enter a number or 'done'." << endl; 28 } 29 30 cout << "Enter your steps (or type 'done' to finish): "; 31 getline(cin, user_input); 32 } 33 34 cout << "\nTotal steps: " << total_steps << endl; 35 cout << "Great job staying active!" << endl; 36 37 return 0; 38}
1#include <iostream> 2#include <string> 3using namespace std; 4 5int main() { 6 int total_steps = 0; 7 string user_input; 8 9 cout << "Enter your steps (or type 'done' to finish): "; 10 getline(cin, user_input); 11 12 while (user_input != "done") { 13 bool is_number = true; 14 15 // Validate input 16 for (char c : user_input) { 17 if (!isdigit(c)) { 18 is_number = false; 19 break; 20 } 21 } 22 23 if (is_number) { 24 int steps = stoi(user_input); 25 total_steps += steps; 26 } else { 27 cout << "Invalid input. Please enter a number or 'done'." << endl; 28 } 29 30 cout << "Enter your steps (or type 'done' to finish): "; 31 getline(cin, user_input); 32 } 33 34 cout << "\nTotal steps: " << total_steps << endl; 35 cout << "Great job staying active!" << endl; 36 37 return 0; 38}

代码示例

1Enter your daily step goal: 5000 2Enter steps taken (or type 'done' to finish): 1200 3Enter steps taken (or type 'done' to finish): 2500 4Enter steps taken (or type 'done' to finish): done 5 6Total steps taken: 3700 7Steps left to reach your goal: 1300 8Keep moving and stay active!

No configuration found for ID: eeca99c807e8e685196b4eed092c77d52354a757fdc6fe19e5284136c2e37e38

1#include <iostream> 2#include <string> 3using namespace std; 4 5int main() { 6 // --- Goal Input --- 7 string goal_input; 8 int goal = 0; 9 10 cout << "Enter your daily step goal: "; 11 getline(cin, goal_input); 12 goal = stoi(goal_input); 13 14 int total_steps = 0; 15 string user_input; 16 17 cout << "Enter steps taken (or type 'done' to finish): "; 18 getline(cin, user_input); 19 20 while (user_input != "done") { 21 bool is_number = true; 22 23 for (char c : user_input) { 24 if (!isdigit(c)) { 25 is_number = false; 26 break; 27 } 28 } 29 30 if (is_number) { 31 int steps = stoi(user_input); 32 total_steps += steps; 33 } else { 34 cout << "Invalid input. Please enter a number or 'done'." << endl; 35 } 36 37 cout << "Enter steps taken (or type 'done' to finish): "; 38 getline(cin, user_input); 39 } 40 41 int steps_left = goal - total_steps; 42 43 cout << "\nTotal steps taken: " << total_steps << endl; 44 cout << "Steps left to reach your goal: " << steps_left << endl; 45 cout << "Keep moving and stay active!" << endl; 46 47 return 0; 48}
1#include <iostream> 2#include <string> 3using namespace std; 4 5int main() { 6 // --- Goal Input --- 7 string goal_input; 8 int goal = 0; 9 10 cout << "Enter your daily step goal: "; 11 getline(cin, goal_input); 12 goal = stoi(goal_input); 13 14 int total_steps = 0; 15 string user_input; 16 17 cout << "Enter steps taken (or type 'done' to finish): "; 18 getline(cin, user_input); 19 20 while (user_input != "done") { 21 bool is_number = true; 22 23 for (char c : user_input) { 24 if (!isdigit(c)) { 25 is_number = false; 26 break; 27 } 28 } 29 30 if (is_number) { 31 int steps = stoi(user_input); 32 total_steps += steps; 33 } else { 34 cout << "Invalid input. Please enter a number or 'done'." << endl; 35 } 36 37 cout << "Enter steps taken (or type 'done' to finish): "; 38 getline(cin, user_input); 39 } 40 41 int steps_left = goal - total_steps; 42 43 cout << "\nTotal steps taken: " << total_steps << endl; 44 cout << "Steps left to reach your goal: " << steps_left << endl; 45 cout << "Keep moving and stay active!" << endl; 46 47 return 0; 48}

逻辑判断:If-Else

for 循环与数组