Simple program can't run completely

Hey guys. I am sorry but I didn't know how to structure this title properly. I have tried running this problem but keep on getting errors and don't know what is causing them. Any help would really be 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>

using namespace std;

int main() 
{
	cout << "\nReminders\n";
	
	string tasks[1000];
	string times[1000];
	int i = 1;
	
	bool addTask = 1;
	
	while (addTask) 
	{
		addTask = 0;
	
		cout << "\nRemind me to: ";
		cin.ignore();	
		getline(cin, tasks[i]);
	
		cout << "\nAt this time (Sun Sep  1 12:00:00 2000): ";
		cin.ignore();
		getline(cin, times[i]);
		i++;
	
		cout << "\nWould you like to add another reminder: ";
		string addTaskChoice;
		cin >> addTaskChoice;
		
		if (addTaskChoice == "Yes" | addTaskChoice == "yes") 
		{
			addTask = 1;
		}
	}
	
	while (1)
	{
		time_t rawtime;
		time ( &rawtime );
		
		int c = 1;
		while (1) 
		{
			if (ctime (&rawtime) == times[c]) 
			{
				cout << tasks[c];
			}
			c++;	
		}
	}
}


Output:

Reminders

Remind me to: Celebrate

At this time (Sun Sep 1 12:00:00 2000): Sun Sep 9 20:34:00 2012

Would you like to add another reminder: No
Run Command: line 1: 31113 Segmentation fault: 11 ./"$2" "${@:3}"


Thanks again!
Last edited on
if (ctime (&rawtime) == times[c])

When c is 1000, you're trying to read off the end of the array.
Oh woops! Didn't notice that! I have fixed that and now it doesn't come out with any error, however it still doesn't print anything when it reaches the given time... Is it finding it difficult to successfully compare ctime (&rawtime) to Sun Sep 1 12:00:00 2000 for example?

*Would it be better to use gmtime()?
Last edited on
Add this right after you stored the time so you can see what you actually stored:

cout << "You stored: "<< times [i] << endl;
I think you have found the problem, as shown below. It isn't saving the whole input and missing out the first character... I have looked through it and can't figure out why.

1
2
3
4
5
6
Reminders

Remind me to: Eat

At this time (eg. Sun Sep  1 12:00:00 2000): 25
You stored: 5


I will continue looking however if you have any idea why that is happening, it would be a great help. Thank you Moschops!
Nevermind! I got it... it was because I used cin.ignore()! Thanks you very much! Let me see if it works now
Perhaps you're telling it to ignore the first letter: cin.ignore();
I seems to be storing the right time however it still won't print anything when the time comes around... I don't know if the time is the same as the string but I printed the time on a different window and copyed and pasted it, adding about a minute and still no output... I don't know what to do. Maybe there is a problem with the cout side of it?
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
/* ctime example */
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
	cout << "\nReminder\n";
	string inputTask;
	string inputTime;
	cout << "Remind myself to: ";
	getline(cin, inputTask);
	cout << "At this time (eg. Sun Sep  1 12:00:00 2000): ";
	getline(cin, inputTime);
	
	bool loop = true;
	while (loop) 
	{
		time_t rawtime;
		time ( &rawtime );
		if (ctime (&rawtime) == inputTime)
		{
			cout << inputTask;
			loop = false;
		}
	}
	return(0);
}


I have tried making it much more simple but it is still not working. Any ideas?
Topic archived. No new replies allowed.