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? –  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

1 Answers1

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
  • 161
  • 1
  • 9
  • I tried adding the space and now it works for the first input(Name of Task) but the rest come together. –  May 01 '22 at 11:18
  • did you add the whitespace for all of them and what do you mean by come together? – lamHoussam May 01 '22 at 11:20
  • I did add a white space for all the scanf statements and now, the first input comes on its own but then the other prompts come all together.. Let me update my question –  May 01 '22 at 11:24
  • I think I see the problem so you want to haave names for tasks not just characters instead of having char name; you should have char name[20/*20 as the legnth of your string */]; this is how strings are defined in C and then instead of using %c in scanf use %s because %c is for chars and %s is for strings when you input a string in the command line and asking for a char in scanf it gives other characters of the string to the other scanfs – lamHoussam May 01 '22 at 11:25
  • so you should have `char name[20]; char category[20]; ...etc ` and in the scanfs use %s instead of %c – lamHoussam May 01 '22 at 11:30
  • Ohhh okayy...I've tried that and now it works but I have this message after each scanf line ```Format specifies type 'char *' but the argument has type 'char (*)[20]'``` –  May 01 '22 at 11:33
  • your scanf should be like this `scanf(" %s", &name);` – lamHoussam May 01 '22 at 11:34
  • Yes thats how it is now –  May 01 '22 at 11:35
  • can you send me your code? – lamHoussam May 01 '22 at 11:35
  • How can I do that? its too long for the comment –  May 01 '22 at 11:36
  • can you send a screenshot? – lamHoussam May 01 '22 at 11:37
  • It doesnt allow me to post pictures in comments –  May 01 '22 at 11:38
  • Ive added it as my answer..The code works the way I want it to but it gives me the error message I mentioned earlier in the comment –  May 01 '22 at 11:39
  • Ok no problem let me send you what I would have done with one of the variables `char name[20]; printf("Name of Task:"); scanf(" %s", &name);` Did you do the same? – lamHoussam May 01 '22 at 11:39
  • Oh ok Ive seen your response – lamHoussam May 01 '22 at 11:40
  • Great please let me know –  May 01 '22 at 11:42
  • Oh you can remove the & before the variable – lamHoussam May 01 '22 at 11:42
  • Oh perfect it works now. Can I know when I should add the & –  May 01 '22 at 11:43
  • & is an operator used to get the pointer to a variable so when you are using scanf you should give it a pointer to the variable in order to give the var a value here name is a pointer to name[20] so you don't need to have the & because writing just name is a way of giving a pointer to name[20] – lamHoussam May 01 '22 at 11:46
  • Oh okayy so the value I input, will still be stored in that variable? –  May 01 '22 at 11:48
  • and you don't need to have '\n' before the printed outputs when you have a scanf because when you use scanf you press '\n' to submit the input so it takes that into consideration – lamHoussam May 01 '22 at 11:50
  • No problem my friend :) – lamHoussam May 01 '22 at 11:50
  • Alright ill add that correction. Thank you so much again –  May 01 '22 at 11:53