0

I'm trying to build a small and first project of mine in C. I want to build a to-do list in list for which I'm using structures, something like

struct tasks{
  int id;
  char desc[150];
}task[100];

I've defined maximum no. of tasks that is 100 and description can take max 150 character.

However, while creating a new task, I'm unable to store a complete sentence even temporarily during the time program is running as after space C thinks it's done; all it takes is the first word of the sentence.

I tried different things like gets(), getchar() but fails to implement it while using in structure.

Can someone point me in the right direction on how I can make the create function which can take the whole sentence as input from the user and not just one word.

edit 1:

so I tried the fgets() and the program compiled correctly with no error yet not functioning correctly.

void create(){
printf("Enter task description"):
fgets(task[i].desc,150,stdin);
printf("%s\n", task[i].desc ); //to check what's going on and all i get is blank space
printf("Task successfully created.\n\n");
}

and the int id part is the second part of the program where I'd like to associate a uid to each task by default along with a time stamp.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Can you share your current input function and the code you tried with `gets` and `getchar` (even though it failed)? Just edit your question and add those code fragments. – aufziehvogel Nov 12 '17 at 22:35
  • If you use `scanf("%s", task[i].desc)`, then it _will_ stop at the end of the first word. So don't use that. You need to show some example input. Does a task occupy one line of input? Can there be more than one task on a line? Can a single task extend over several lines. [Don't ever use `gets()` — it is too dangerous!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) Using `fgets()` might be sensible, but a lot depends on your answers to my questions. Does the user have to specify the ID number? Avoid using `getchar()` if you can. – Jonathan Leffler Nov 12 '17 at 23:01
  • 2
    You're probably using `scanf` somewhere. Don't use `scanf` for user input. – melpomene Nov 19 '17 at 23:01
  • thank you @JonathanLeffler for your help and sorry for late reply my exams are going on and this a side project I'm doing to learn to programme. No there will be a single task in each line and when enter is pressed it should be submitted that is why I'm trying to make an array of struct. Each struct will be a new task. and no I want the ID to be generated and associated automatically that's the second part, which I've not given much thought as I'm stuck at the starting. – Mohit Kumar Nov 19 '17 at 23:08
  • An melpomene says, I too suspect you're using `scanf` somewhere else, and probably have a newline still left in the input buffer when `fgets` comes around. It may not necessarily be `scanf` particularly, but that's my guess. Either way, it's probably not in the part of the program that you've posted that the error is. – Dolda2000 Nov 19 '17 at 23:57

2 Answers2

0

Easy overlook for beginners: is the variable "i" well defined or is it some wild garbage value that will "buffer overflow" your array?

Passing in "i" as an argument to create() would be the way to go:

void create(int i){...}

Stack variables are not zeroed by default and therefore inherit whatever values that were already there.

Gilbert
  • 3,740
  • 17
  • 19
  • Careful, objects with *static* or *thread* storage duration are zeroed by default. See [C11 Standard (draft n1570) - 6.7.9 Initialization](http://port70.net/~nsz/c/c11/n1570.html#6.7.9) – David C. Rankin Nov 20 '17 at 04:36
  • Well yes, for sure. Thus my comment on the stack. And even in static variables if the routing was run twice you would be in trouble the second pass without initialization. – Gilbert Nov 20 '17 at 12:59
  • @Gilbert I tried what you said but now instead of blanks, I'm getting Segmentation fault (core dumped). also, i've declared i as the global variable and initialized it also. – Mohit Kumar Nov 20 '17 at 19:52
0

so I was able to take the whole sentence as an input by the help of answer given by @david as you can see in the code below:

void create(int i){
printf("Enter task description:\n");
fgets(task[i].desc,150,stdin);
fflush(stdin);
fgets(task[i].desc,150,stdin);
printf("%s\n", task[i].desc );
printf("Task successfully created.\n");
}

but fgets alone was not enough as with only one fgets function console was not taking any input, instead all i was getting was a blank line. But adding fflush(stdin) and then another fgets did the magic.