Program using array won't compile properly

Hey. I am trying to create a program that stores reminders or 'tasks' and a time associated with them and shown below, however it won't compile properly. I still prints everything up until the first input. Any help would be much appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int main() 
{
	cout << "\nReminders\n\n";
	
	string tasks[0];
	string times[0];
	int i = 1;
	
	bool addTask = 1;
	
	while (addTask) 
	{
	addTask = 0;
	
	cout << "Remind me to: ";
	cin.ignore();
	getline(cin, tasks[i]);
	
	cout << "\nAt this time: ";
	cin.ignore();
	getline(cin, times[i]);
	i++;
	
	cout << "\n\nWould you like to add another reminder: ";
	string addTaskChoice;
	cin >> addTaskChoice;
	
	if (addTaskChoice == "Yes" | addTaskChoice == "yes") 
	{
		addTask = 1;
	}
	}
}


This is the output:


Reminders

Remind me to: Eat
Run Command: line 1: 27973 Segmentation fault: 11 ./"$2" "${@:3}"


Thanks!
In lines 10 and 11, you are making an array with no elements, so in line 22, when you add a task to the array, it causes a memory fault.

Edit:Try changing the 0 to a number like 10 or 50, depending on how many reminders you want at max. Or use vectors.
Last edited on
How did you expect arrays of size 0 to be useful? Actually, it's not even allowed in C++.
Last edited on
I assumed that it would just increase in size... Thank you ! :D
I agree with Peter87
I assumed that it would just increase in size... Thank you ! :D


Consider using a vector, with the push_back function. Look at it in the reference section - top left of this page
Topic archived. No new replies allowed.