0

I'm quite new to C programming and I have an issue with how the code prompts the input. I need the inputs under the input statement to come one by one. Now the way the prompts are output is:

To add a new task enter the details below
Name of Task: Science

Then all the other inputs come as a group

Category of Task:
Information about task:
Due Date of Task:
Status of Task
 TD = To-Do
 IP = In Progress
 CT = Completed Task
Enter Status:

But I want to it ask for the name of the task first and once I input that information, it should ask the category For example:

To add a new task enter the details below
Name of Task:

This is my code:

#include<stdio.h>

int main() {
    char main_choice;
    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");
    printf("\nEnter your choice:");
    scanf("%c", &main_choice);
    
    if (main_choice == 'A'){
        char name;
        char category;
        char info;
        char date;
        char status;
        printf("\nTo Add a new task enter the details below\n");
        printf("Name of Task:");
        scanf(" %c", &name);
        printf("\nCategory of Task:");
        scanf(" %c", &category);
        printf("Information about task:");
        scanf(" %c", &info);
        printf("Due Date of Task:");
        scanf(" %c", &date);
        printf("Status of Task\nTD = To-Do\nIP = In Progress\nCT = Completed Task\n Enter Status:");
        scanf(" %c", &status);
    }

    return 0;
} 
  • Almost always: use `fgets` to get a line of input, then parse that line. If you want to use `scanf`-like functionality you can use `sscanf`. – Cheatah May 01 '22 at 11:06
  • When I use ```sscanf``` it says Format string is not a string literal and for ```fgets``` do I just replace scanf with that? – Hannah Joshua May 01 '22 at 11:10
  • Use a blank space in your `scanf` like `scanf(" %c", &name);` – Avinash May 01 '22 at 11:13
  • Check this answer to read more: https://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c – Avinash May 01 '22 at 11:14

2 Answers2

0

When using scanf in C you should put a whitespace before the %c like this:

scanf(" %c", &name);

It is required to put space before %c to skip the white space in buffer memory. so that %c matches the character given instead of the space char.

lamHoussam
  • 146
  • 1
  • 6
-1

This the corrected working code from the question I asked

#include<stdio.h>

int main() {
    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);
    
    if (main_choice == 'A'){
        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("\nCategory of Task:");
        scanf(" %s", category);
        printf("\nInformation about task:");
        scanf(" %s", info);
        printf("\nDue Date of Task:");
        scanf(" %s", date);
        printf("\nStatus of Task\nTD = To-Do\nIP = In Progress\nCT = Completed Task\n Enter Status:");
        scanf(" %s", status);
    }

    return 0;
}

  • 2
    I'm not quite sure about this post. Is it a self-answer, or is it an 'edit' to your question? If the former, please add some explanatory text, explaining that; if it's the later, please remove it and edit your question. – Adrian Mole May 01 '22 at 21:16