Home > Software design >  How to prompt inputs one by one in C?
How to prompt inputs one by one in C?

Time:05-02

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;
} 

CodePudding user response:

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.

CodePudding user response:

#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;
}

  •  Tags:  
  • c
  • Related