-1

I'm writing a simple task reminder program in c which prints the given task after the certain amount of time. Here is a small portion of the code that I'm having problem with. Basically I'm having trouble with scanf() as the function is acting strangely.

#include <stdio.h>
#include <time.h>

int main(){
   int hour,minute,curr_time,end_time;
   printf("input the hour and minute after which alarm will start in HH:MM : \n");
   scanf("%d:%d", &hour,&minute);
   char task[50];
   printf("Name of the task: \n");
   scanf("%s" , task);
   printf("your task is %s" , task);

return 0;
}

Now when I compile and run the program , the following occurs.

~$ ./a.out
input the hour and minute after which alarm will start in HH:MM : 
00.56
Name of the task: 
your task is .56

I cannot input the name of the task. As soon as I finish giving the hour and minute , the program ends without taking the task input.

Shiladitya Bose
  • 893
  • 2
  • 13
  • 31
  • Possible duplicate of [Reading a line using scanf() not good?](http://stackoverflow.com/questions/17294809/reading-a-line-using-scanf-not-good) – dandan78 Oct 29 '15 at 15:09
  • My compilation (MSVC) works perfectly well when I enter the time as prompted. The `%s` format spec ignores whitespace, so the `scanf` function is not satisfied as long as I keep tapping "Enter", I have to enter some text, which is truncacted at the first `space` character. – Weather Vane Oct 29 '15 at 15:13

3 Answers3

1

You're using : as separator in scanf but while entering you're putting a decimal. Since scanf is expecting integers, it stops scanning at the first decimal point.

You can see the what the value of hours and minutes is by printing them

#include <stdio.h>
#include <time.h>

int main(){
   int hour,minute,curr_time,end_time;
   printf("input the hour and minute after which alarm will start in HH:MM : \n");
   scanf("%d:%d", &hour,&minute);
   char task[50];
   printf("Name of the task: \n");
   scanf("%s" , task);
   printf("your task is %s" , task);
   printf("hour is %d" , hour);
   printf("minute is %d" , minute);

return 0;
}

Output:

input the hour and minute after which alarm will start in HH:MM : 
00.56
Name of the task: 
your task is .56
hour is 0
minute is 0

Either change the separator in scanf to a decimal or input your hours and minute as 00:56

input the hour and minute after which alarm will start in HH:MM : 
00:56
Name of the task: 
test
your task is test
hour is 0
minute is 56
Kartik Anand
  • 4,513
  • 5
  • 41
  • 72
0

Look into using FGETs instead of scanf. Right now after you are entering in the time, you are leaving a newline character in the buffer. When scanf goes to read in a string, it grabs the newline character. Making it seem as if it skipped your input.

scerrecrow
  • 259
  • 1
  • 9
  • The program works perfectly well when the time is entered in the correct format HH:MM. The `%s` format does not "grab the newline" in fact it refuses to accept a `newline`. – Weather Vane Oct 29 '15 at 15:39
0

Your code have only the careless mistake.That is the absence of & symbol in scanf.

& have a special role in scanf. It represents the address where the value need to be store.

But,this absence doesn't show any error.

Due to this reason,you get an output as your last entered value until reach new line character i.e. .56

Be conscious,in some case it makes malfunctioning of entire program.