Rayan Ali
Rayan Ali

Reputation: 13

How to jump back to main from a function?

I need to jump back to main from function create_task to main. I labeled where I want to jump to and from where. is there logic for it or can we use a built-in function? goto statements aren't working. calling the main function stops the program.

#include<stdio.h>
#include<string.h>
struct task{
char t_name[99];
char task_noted[999];
};
void create_task(struct task *arrayt );
int main(){

struct task arrayt[999];
//i want to get here!!!
int check=0;
while(check>3||check<1){
    printf("Enter 1 to create a task\nEnter 2 to delete a task\nEnter 3 to veiw task:\n");
    scanf("%d",&check);
}

switch(check){
    case 1:
        printf("CREATING A TASK\n");
        create_task(arrayt);
        break;
    case 2:
        printf("DELETE TASK");
        break;
    case 3:
        printf("VEIW TASKS");
        break;
    default :
        printf("INVALID OPTION!!!!");
        break;
}
}
void create_task(struct task *arrayt ){
int i=0,want=0;
FILE *ptr;
while(i!=999){
    if(arrayt[i].t_name!= NULL && arrayt[i].task_noted!= NULL){
        
        ptr = fopen("data.txt","a");
        printf("Enter Task Name: ");
        scanf("\n%[^\n]s",arrayt[i].t_name);
        fprintf(ptr,"\n%s",arrayt[i].t_name);
        printf("\nEnter Task Data:\n");
        scanf("\n%[^\n]s",arrayt[i].task_noted);
        fprintf(ptr,"\n%s",arrayt[i].task_noted);
        i++;
        printf("\nDo you want to add another task?\nPRESS 1 FOR ANOTHER\nPRESS 2 TO EXIT\n");
        scanf("%d",&want);
        if(want==2){
            //i want to goto main from here!!!!
        }
        fclose(ptr);
        
    }
}
}

Upvotes: 0

Views: 694

Answers (5)

Eric Postpischil
Eric Postpischil

Reputation: 222362

Your problem is not really that you want to “jump back” to main. What you want is to have a loop reading and processing user input. To do that, make a loop using a while statement:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // Put needed declarations here.

    while (1)
    {
        // Prompt user and read input.
        printf("Enter 1 to create a task.\n...\nEnter 0 to stop.\n");
        int check;
        int result = scanf("%d", &check);

        // Test whether scanf worked.  End the program if it failed.
        if (result != 1)
        {
            fprintf(stderr, "scanf failed.\n");
            exit(EXIT_FAILURE);
        }

        // If the user entered 0 to stop the loop, stop the loop.
        if (check == 0)
            break;

        // Dispatch based on the user's request.
        switch (check)
        {
            case 1:
                // Do stuff for case 1.
                break;
            case 2:
                // Do stuff for case 2.
                break;
            case 3:
                // Do stuff for case 3.
                break;

            // Handle incorrect input from user.
            default:
                printf("Error, %d is not a valid choice.\n", check);
                break;
        }
    }
}

Upvotes: 0

I need to jump back to main from function create_task to main.

Another possibility could be to carefully use setjmp(3) with longjmp.

Beware, they are confusing (since longjmp is shrinking your call stack) and might make your code unreadable. Be sure to document and comment more your code. You may need to use fflush(3) a bit more or to end your printf format control strings with a \n. See also setvbuf(3).

As a rule of thumb, longjmp should be rarely used. It is some kind of exception handling in C.

The difficult issue could be to "undo" important side effects (in particular, C dynamic memory allocation) done between a setjmp and its corresponding longjmp. You'll need some good code review in that case.


   ptr = fopen("data.txt","a");

Seems wrong (since in your code you forgot to test ptr). What would happen when fopen(3) fails (this can happen when running your program in a different working directory)? Further calls to fprintf(3) could crash. Beware of undefined behavior.


    printf("DELETE TASK");

I recommend coding printf("DELETE TASK\n"); which might avoid buffering and show that message earlier in your terminal.

A related concept is continuation and continuation passing style (in C, with callbacks).

Be sure to read Modern C and look into this C reference website (and later inside some C draft standard such as n2176). Read also the documentation of your C compiler (e.g. GCC) and debugger (e.g. GDB). I suggest to compile your code with gcc -Wall -Wextra -g (all warnings and debug info), then to use GDB to understand its behavior. On Linux, you could also use ltrace(1).

Upvotes: 0

Orion
Orion

Reputation: 245

  • If you are thinking about using the jumping statements like goto, break, and continue they won't help you to jump from one function to another. That is because the two functions have different scopes. You can use a return statement if you want the control to be returned to the main().

  • if you declare the function void function (...), then you can simply return at any point, or allow control to run off the end of the function -- the compiler will automatically return to the calling function

  • if you declare the function to return a value int function(...) then you must return XXXand XXX type must match the return type of the function

Upvotes: 0

rktejesh
rktejesh

Reputation: 70

Add an if statement at the beggining of main with a bool set to false and when want is 2 set it to true.

Upvotes: 0

ruohola
ruohola

Reputation: 24038

You should use the return statement, and not gotos or anything like that:

if (want == 2) {
    return;
}

Upvotes: 1

Related Questions