這就是我到目前爲止所做的。當我嘗試將項目添加到數組時,程序崩潰。我正在尋找它,最終存儲該項目,並獲取當前時間,並將該項目標記爲未完成,直到用戶輸入「完成」。C++結構和動態分配陣列
不能使用 - 標準集裝箱或智能指針
#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;
}
代碼中沒有數組。 – juanchopanza
您正在使用下標間接指向單個結構的指針。 –
你能詳細說明你的代碼「不起作用」嗎?你在期待什麼,究竟發生了什麼? **如果您遇到異常或錯誤,請發佈其發生的行和詳細信息。**請[編輯]這些詳細信息,否則我們可能無法提供幫助。 –