I am trying to make a program that uses the <ctime>
library to give the option to show the year/month/day/hour/minutes/seconds. The problem is that after entering option
on main()
the program says Segmentation fault: 11
.
What a I doing wrong?.
time.cpp
:
int time(int x) {
time_t now = time(0);
tm *ltm = localtime(&now);
switch (x) {
case 1: // year
return 1900 + ltm -> tm_year;
break;
case 2: // month
return 1 + ltm -> tm_mon;
break;
case 3: // day
return ltm -> tm_mday;
break;
case 4: // hour
return 1 + ltm -> tm_hour;
break;
case 5: // minutes
return 1 + ltm -> tm_min;
break;
case 6: // seconds
return 1 + ltm -> tm_sec;
break;
default:
return 0;
break;
}
}
program.h
:
#pragma once
int time(int);
main.cpp
:
int main() {
int option;
cout << "Chose an option:\n[1] Year\n[2] Month\n[3] Day\n[4] Hour\n[5] Minutes\n[6] Seconds\n -";
cin >> option;
if (option > 6) {
cout << "The options are from 1-9\n\n";
main();
}
cout << time(option) << endl;
}
PS:
1. <ctime>
is only included on time.cpp
2. I am using namespace std;