【发布时间】:2026-01-20 10:55:02
【问题描述】:
这是我目前所拥有的。当我尝试向数组添加项目时,程序崩溃。我正在寻找它来最终存储项目,以及获取当前时间,并将项目标记为未完成,直到用户输入“完成”。
不能使用 - 标准容器或智能指针
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
struct List{
string items;
char completed;
int time_t;
};
int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;
int count = 0;
List* list = new List[];
// Get current time
time_t recordedTime = time(nullptr);
tm localTime = *localtime(&recordedTime);
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Mark as Completed" << endl;
cout << "5. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cout << endl;
// Perform the operation
switch(userChoice)
{
case 1:
{
cin.ignore(50, '\n');
cout << "Enter one item per line, or enter 'done' to finish\n";
while(true)
{
do{
count++;
cout << "Item" << count << ": ";
getline(cin, list[count].items);
}
while(list[count].items !="done");
if(list[count].items == "done")
{
break;
}
}
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
case 4:
{
cout << "Item #: ";
}
break;
case 5:
{
return 0;
}
}
// Output the list
cout << "-------Items-------" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
return 0;
}
【问题讨论】:
-
您的代码中没有数组。
-
您正在对指向单个结构的指针使用下标间接。
-
您能否详细说明您的代码“不起作用”的原因?你期待什么,实际发生了什么? 如果您遇到异常或错误,请发布它发生的行和详细信息。请edit这些详细信息,否则我们可能无法提供帮助。
-
我得到的错误是在第 24 行的
的调试中 -
List* list = new List[];格式不正确(一些 cmets 建议分配 1 大小的列表,但事实并非如此)