0

I'm very new to C programming and I'm trying to create a simple task management system. But this code doesn't run and it doesn't show me what the error is too. Am I doing something wrong with the functions? This part of the code is just me trying to get a choice from the user and then divert them to different functions according to their choice.

#include<stdio.h>

void add_task(void);
void view_task(void);
void manage_task(void);

int main()
{
    void main_menu(void);
    char main_choice;
    printf("Welcome to your Task Management Sytem\n");
    printf("What would you like to do today?\n A: Add New Task\n B: View Task \n C: Manage Tasks\n");
    printf("\nEnter your choice:");
    scanf("%c", &main_choice);
    
    switch(main_choice)
    {
        case 'A':
            printf("\n--------------ADD A NEW TASK-------------");
            add_task();
            break;
        case 'B':
            printf("\n----------VIEW YOUR TASKS----------");
            view_task();
            break;
        case 'C':
            printf("\n----------MANAGE YOUR TASKS----------");
            manage_task();
            break;
    }
    
    void add_task(void);
    char name[20];
    char category[20];
    char info[20];
    char date[20];
    char status[20];

    printf("\nTo Add a new task enter the details below\n");
    printf("Name of Task:");
    scanf(" %s", name);
    printf("Category of Task:");
    scanf(" %s", category);
    printf("Information about task:");
    scanf(" %s", info);
    printf("Due Date of Task(dd/mm/yyyy):");
    scanf(" %s", date);
    printf("Status of Task\n TD = To-Do\n IP = In Progress\n CT = Completed Task\nEnter Status:");
    scanf(" %s", status);
    
    
    void view_task(void);
    char task_choice;
    printf("View Tasks by:\nA:Due Date\nB:Category\nC:Status\nD:View All Tasks");
    scanf(" %c", &task_choice);
    if (task_choice == 'A')
    {
        char date_choice;
        printf("A:View Tasks in ascending order\nB: View Tasks in descending order\nEnter your choice:");
        scanf(" %c", &date_choice);
    

    }
    return 0;
}

  • 1
    Note: space before `"%s"` serves no purpose in `scanf(" %s", ...);`. Lack of a _width_ makes `scanf(" %s", ...);` worse than [`gets()`.](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Better as `scanf("%19s", ...);` – chux - Reinstate Monica May 03 '22 at 03:26
  • without the space, the inputs don't come one at a time –  May 03 '22 at 03:27
  • 1
    Please be more specific what you mean by "The code doesn't run". Does an error occur before it prints the Welcome message? What is the error message? What happens if you run the program under the debugger? – Raymond Chen May 03 '22 at 03:28
  • Hannah Joshua, Sorry you are mistaken with `"%s"`. – chux - Reinstate Monica May 03 '22 at 03:29
  • 1
    The only function in your code is `main`. – user3386109 May 03 '22 at 03:29
  • Theres no error message but on Xcode, the pop says 'Build Failed' –  May 03 '22 at 03:30
  • The error message is "Undefined symbols: _add_task, _manage_task, _view_task". It's a linker error, and you are correct that linker errors are not easily seen in Xcode. – user3386109 May 03 '22 at 03:36
  • Yes...but im not sure how to fix it –  May 03 '22 at 03:39
  • 2
    You need to reread the chapter on functions. – user3386109 May 03 '22 at 03:39
  • regarding statements like: `void view_task(void);` This is the 'signature' line of a function. not a prototype for the function. Therefore, that trailing `;` should not be there – user3629249 May 03 '22 at 05:07
  • BTW: functions must not be nested inside other functions – user3629249 May 03 '22 at 05:08
  • 2
    You need to study how functions work before using them, simple as that. You cannot learn C by guessing or trial & error. – Lundin May 03 '22 at 06:56

1 Answers1

0

regarding:

int main()
{
void main_menu(void);
char main_choice;
printf("Welcome to your Task Management Sytem\n");
printf("What would you like to do today?\n A: Add New Task\n B: View Task \n   C: Manage Tasks\n");
printf("\nEnter your choice:");
scanf("%c", &main_choice);

switch(main_choice)
{
    case 'A':
        printf("\n--------------ADD A NEW TASK-------------");
        add_task();
        break;
    case 'B':
        printf("\n----------VIEW YOUR TASKS----------");
        view_task();
        break;
    case 'C':
        printf("\n----------MANAGE YOUR TASKS----------");
        manage_task();
        break;
}

The following is one example of the changes needed in the code:

Separate the function: main_menu().

Note: Each of the other sub functions should receive the same treatment.

#include <ctypes.h>   // includes function: toupper()   

void main_menu(void)
{
    char main_choice = 'z';  // 'Z' is just a place holder

    while( main_choice != 'Q' )
    {
        printf("Welcome to your Task Management System\n");
        printf("What would you like to do today?\n"
               "A: Add New Task\n"
               "B: View Task\n"
               "C: Manage Tasks\n"
               "Q: Quit\n");  

        printf("\nEnter your choice:");
        scanf(" %c", &main_choice);
        
        switch( toupper(main_choice) )
        {
        case 'A':
        printf("\n--------------ADD A NEW TASK-------------");
        add_task();
        break;
    
        case 'B':
        printf("\n----------VIEW YOUR TASKS----------");
        view_task();
        break;
    
        case 'C':
        printf("\n----------MANAGE YOUR TASKS----------");
        manage_task();
        break;

        default:
        printf( "unknown choice, try again\n" );
        break;
        }
    }
    return;
}


int main( void )
{
     main_menu();
}

    
user3629249
  • 16,402
  • 1
  • 16
  • 17