Menu
CoddyTech

添加任务

本课属于 Coddy C++ 学习旅程的“逻辑与流程”单元。第 19 课,共 56 课。

challenge icon

挑战

简单

通过实现向系统添加新任务的功能,扩展你的任务列表工具。本课将在上一课的基础上,加入用户交互和任务存储功能。

将提供以下输入:

  • 一个表示要添加到列表中的任务描述的字符串

你的程序应当:

  1. 使用与上一课相同的设置启动(名为 tasks 的空 std::vector<std::string> 和欢迎消息)
  2. 从输入中读取任务描述
  3. 使用 push_back() 将任务描述添加到 tasks 向量中
  4. 打印确认消息,说明任务已成功添加
  5. 使用向量的 .size() 方法显示列表中的当前任务数量

使用以下确切的输出格式:

Welcome to Task List Tool!

Menu Options:
1. Add Task
2. View Tasks
3. Quit

Task list system initialized and ready!
Task "[task description]" added successfully!
Total tasks: [number of tasks]

本练习展示了向量如何随着元素的添加而动态增长,以及 .size() 方法如何反映当前存储的项目数量。确认消息会告知用户操作已成功完成。

亲手试试

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> tasks;
    
    cout << "Welcome to Task List Tool!" << endl;
    cout << endl;
    cout << "Menu Options:" << endl;
    cout << "1. Add Task" << endl;
    cout << "2. View Tasks" << endl;
    cout << "3. Quit" << endl;
    cout << endl;
    cout << "Task list system initialized and ready!" << endl;
    
    return 0;
}

逻辑与流程中的所有课

自己练习: C++ 在线编译器