I have this program that I have been having problems with now for the last couple of days. I think I am nearly there but there seems to be one last problem to tend to. If anyone knows how to fix the error that keeps on appearing I would really appreciate it! Thanks
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
usingnamespace std;
int main()
{
string reminders[100]; // A maximun of 100 reminders
int time[6][100]; // A maximun of a 100 times (one for each reminder) and 6 stored values for each time (hh:mm:ss)
bool addReminder = true;
int i = 0;
while (addReminder)
{
addReminder = false;
cout << "Remind me to: ";
getline(cin, reminders[i]);
cout << "At this time (eg. hh:mm:ss, using 24 hour clock): ";
string inputTime;
getline(cin, inputTime);
time[0][i] = inputTime[0] - '0'; // Converts inputTime into a int
time[1][i] = inputTime[1] - '0';
time[2][i] = inputTime[3] - '0';
time[3][i] = inputTime[4] - '0';
time[4][i] = inputTime[6] - '0';
time[5][i] = inputTime[7] - '0';
i++;
cout << "\nWould you like to add another reminder: ";
string addTaskChoice;
cin >> addTaskChoice;
if (addTaskChoice == "Yes" | addTaskChoice == "yes")
{
addReminder = true;
}
cin.ignore();
}
bool loop = true;
int c = 0;
while (loop)
{
int seconds = time(NULL);
while (seconds > 86400) // Turning time since Jan 1 1970 into seconds since the beginning of the day
{
seconds -= 86400;
}
int inputSeconds = time[0][c] * 36000 + time[1][c] * 3600
+ time[2][c] * 600 + time[3][c] * 60 + time[4][c] * 10 + time[5][c]; // Turn user inputed time into time since beginning of the day
if (seconds > inputSeconds) // Check if the time is larger than the time inputed
{
cout << reminders[c];
}
if (c < 99)
{
c++;
}
else
{
c = 0;
}
sleep(1);
}
}
This is the error:
1 2 3 4
reminder program 2.0.cpp:50:21: error: called object type 'int (*)[100]' is not a function or function pointer
int seconds = time(NULL);
~~~~^
1 error generated.
int time[6][100]; // <- you have an array named 'time'
...
int seconds = time(NULL); // <- then you try to do this
Since 'time' is the name of your array, it is using your array and not the time function. Which is why you get an error.
Possible solutions:
1) rename your array to something else.
2) use the scope resolution operator to indicate you want to use the global 'time' function and not the local 'time' array:
int seconds = ::time(NULL); // <- the :: means use the global name
EDIT:
This is also an example of why you should avoid dumping the std namespace into the global namespace.
If you did this "the right way" without usingnamespace std; then your code would have looked something like this:
1 2 3 4 5 6 7 8 9 10 11
#include <ctime> // <- ctime, not time.h
// using namespace std; <- don't do this.
//...
int time[6][100]; // this is fine
//...
int seconds = std::time(NULL); // use std's time
namespaces are there to avoid these kinds of conflicts.
@Disch
This is also an example of why you should avoid dumping the std namespace into the global namespace.
If you did this "the right way" without using namespace std; then your code would have looked something like this:
1 2 3 4 5 6 7 8 9 10 11 12
#include <ctime> // <- ctime, not time.h
// using namespace std; <- don't do this.
//...
int time[6][100]; // this is fine
//...
int seconds = std::time(NULL); // use std's time
namespaces are there to avoid these kinds of conflicts.
I am afraid that you are wrong. It is not an example why the using directive should be avoided because time is a C standard function and the implementation may itself place it into the global namespace.:)
Hey, I changed the time[x][y] to times and now there is no error. However, it is still not alerting me when it passes the time... There is a blinking line allowing me to enter more information even after i tell the program that I don't and when the time comes around nothing happens. I don't possibly know what to anymore as if have changed it to raw time and everything. Any ideas would be a massive help
int main()
{
string reminders[10]; // A maximun of 100 reminders
int times[6][10]; // A maximun of a 100 times (one for each reminder) and 6 stored values for each time (hh:mm:ss)
bool addReminder = true;
int i = 0;
while (addReminder == true)
{
addReminder = false;
cout << "Remind me to: ";
getline(cin, reminders[i]);
cout << "At this time (eg. hh:mm:ss, using 24 hour clock): ";
string inputTime;
getline(cin, inputTime);
times[0][i] = inputTime[0] - '0'; // Converts inputTime into a int
times[1][i] = inputTime[1] - '0';
times[2][i] = inputTime[3] - '0';
times[3][i] = inputTime[4] - '0';
times[4][i] = inputTime[6] - '0';
times[5][i] = inputTime[7] - '0';
i++;
cout << "\nWould you like to add another reminder: ";
string addTaskChoice;
cin >> addTaskChoice;
if (addTaskChoice == "Yes" | addTaskChoice == "yes")
{
addReminder = true;
}
cin.ignore();
}
int c = 0;
while (1)
{
cout << "hello"; // To test if this loop has begun
time_t seconds;
seconds = time(NULL);
while (seconds > 86400) // Turning time since Jan 1 1970 into seconds since the beginning of the day
{
seconds -= 86400;
}
int inputSeconds = times[0][c] * 36000 + times[1][c] * 3600
+ times[2][c] * 600 + times[3][c] * 60 + times[4][c] * 10 + times[5][c]; // Turn user inputed time into time since beginning of the day
if (seconds > inputSeconds) // Check if the time is larger than the time inputed
{
cout << reminders[c];
}
if (c < 9)
{
c++;
}
else
{
c = 0;
}
sleep(1);
}
}
Output:
1 2 3 4 5 6
Remind me to: Eat
At this time (eg. hh:mm:ss, using 24 hour clock): 19:55:00
Would you like to add another reminder: No
note it has not printed "hello"
I think it is stuck in the first loop but can't put my finger on why. Also tried with <ctime> and <time.h> neither work
@vlad: the <ctime> header puts it in the std namespace.
the <time.h> header does not.
Though I think <ctime> also puts it in the global namespace? Not sure. Either way the complication is avoided if you prefix standard library calls with std:: and don't use using namespace std which was my original point.
Mind you this method of storing the time for each reminder has got us
scratching our head - seriously??
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int time[6][100]; // A maximun of a 100 times (one for each reminder) and 6 stored values for each time (hh:mm:ss)
//other code here
cout << "At this time (eg. hh:mm:ss, using 24 hour clock): ";
string inputTime;
getline(cin, inputTime);
time[0][i] = inputTime[0] - '0'; // Converts inputTime into a int
time[1][i] = inputTime[1] - '0';
time[2][i] = inputTime[3] - '0';
time[3][i] = inputTime[4] - '0';
time[4][i] = inputTime[6] - '0';
time[5][i] = inputTime[7] - '0';
I will get rid of using the namespace std and see if that works. And sorry if the method has got you scratching you head haha, I though the comments might have helped but I guess they didnt.
I added this and got rid of the namespace std;. I added this so that it would not compare the times if the reminder was empty. And then printed the output of the time it thinks it is compared with the time that I inputed.
while (1)
{
time_t seconds;
seconds = std::time(NULL);
while (seconds > 86400) // Turning time since Jan 1 1970 into seconds since the beginning of the day
{
seconds -= 86400;
}
if (reminders[c] != "")
{
int inputSeconds = (times[0][c] * 10 + times[1][c]) * 3600
+ (times[2][c] * 10 + times[3][c]) * 60 + (times[4][c] * 10 + times[5][c]);//Turn user inputed time into time since beginning of the day
if (seconds > inputSeconds) // Check if the time is larger than the time inputed
{
std::cout << reminders[c];
}
std::cout << seconds << " / " << inputSeconds << std::endl;
}
if (c < 9)
{
c++;
}
else
{
c = 0;
}
sleep(1);
}
This is the output:
1 2 3 4 5 6 7 8
Remind me to: Eat
At this time (eg. hh:mm:ss, using 24 hour clock): 07:50:00
Would you like to add another reminder: No
24585 / 28200
24595 / 28200
24605 / 28200
24615 / 28200
The time inputed has been correctly changed into seconds since the start of the day however the current seconds from the start of the day are wrong (left hand side). I am trying to figure out why. Maybe the universal clock started in a different time zone? Trying to get my head around it