0

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;

Ilan15
  • 27
  • 4
  • 4
    You can't call `main` in your program. – cigien May 12 '20 at 16:27
  • 1
    As mentioned, in C++ it's not allowed to call [the `main` function](https://en.cppreference.com/w/cpp/language/main_function) yourself. If you need a loop, use an actual loop. – Some programmer dude May 12 '20 at 16:29
  • 4
    Your time function calls itself before doing anything else - that'll never return. Real bad idea to call your functions the same as stdlib functions. – Mat May 12 '20 at 16:29
  • 1
    Calling `main` yourself is [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub). – Jesper Juhl May 12 '20 at 16:29
  • 3
    As for your problem, which `time` function are you really calling? Your own or the `std::time` function? Think about what happens if your `time` function calls itself infinitely... – Some programmer dude May 12 '20 at 16:30
  • @Someprogrammerdude About your duplicate: there's actually no `using namespace std;` in the code OP is showing. The problems are simply infinite recursion and UB (due to calling `main`). So, I don't agree with that duplicate and vote to reopen. – Jesper Juhl May 12 '20 at 16:33
  • 1. I am `using namespace std;` I just thought that it was irrelevant to put it here. 2. I deleted the `main() ` and the problem continues. – Ilan15 May 12 '20 at 16:47
  • @Ilan15 then please edit your post to include the new problem. Where error occurs if you don't call to ```main()```? – Roim May 12 '20 at 17:20

1 Answers1

1
int time(int x) {
    time_t now = time(0);

The first thing your time function does is call itself. This will eventually overflow the stack.

Also, don't call main and don't put a break after a return.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278