0

On executing the code below, the output comes out to be

Enter activity selected: hello

After taking input for number of activities and entering the activities . The scanf() is getting completely skipped.

I tried everything found on the internet like using getchar() instead of scanf(), putting a getchar() before taking input and using fflush().

But nothing seems to be working. Can anyone tell what is the mistake I am making?

#include<stdio.h>
#include<stdlib.h>

typedef struct
{
    char name;
    int start;
    int finish;
}activity;

int main()
{
    int n;
    printf("Enter number of activities: ");
    scanf("%d",&n);
    
    activity* a = (activity*)malloc(sizeof(activity)*n);
    printf("Enter activities: ");
    
    for(int i=0;i<n;i++)
    {
        scanf("%c %d %d ",&(a[i].name),&(a[i].start),&(a[i].finish));
    }
    
    //getchar();
    char ch;
    int s,f;
    printf("Enter activity selected:");
    //fflush(stdin);
    //scanf(" %c",&ch);
    ch = getchar();
    printf("hello");
    return 0;
}
IrAM
  • 1,720
  • 5
  • 18
jenny jenkins
  • 95
  • 1
  • 9
  • 2
    You don't have a space before the `%c`, so it reads the newline left behind by the previous `scanf`. – user3386109 Dec 08 '20 at 08:11
  • why did you comment `scanf(" %c",&ch);` , that will wait for you to enter, with `ch = getchar();` you will have to do it manually – IrAM Dec 08 '20 at 08:12
  • `//scanf(" %c",&ch);` Here there is a space before `%c`. – jenny jenkins Dec 08 '20 at 08:13
  • I was referring to the `scanf("%c` in the loop. Btw, you should be checking the return value from `scanf` to make sure that it worked. – user3386109 Dec 08 '20 at 08:14
  • @IrAM I was justing testing out `ch = getchar()`, after `scanf(" %c",&ch);` didn't work. – jenny jenkins Dec 08 '20 at 08:14
  • Since you ask the user to enter complete lines (I guess), I would advice you to use `fgets` + `sscanf`, to divide the problem in smaller parts. Moreover, you should check what is returned by `*scanf`. And to finish, there is some good reading: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – Mathieu Dec 08 '20 at 08:16
  • as suggested above you need to give a space before `%c` in `scanf("%c %d %d ",&(a[i].name),&(a[i].start),&(a[i].finish));` also – IrAM Dec 08 '20 at 08:16
  • @user3386109 That's not working either, neither is using `getchar()` after the loop to eat up the newline. – jenny jenkins Dec 08 '20 at 08:19
  • You cannot use the same variable in `scanf("%c",%ch)` and for `ch=getchar()`. In the first you need a `char` while the second requires an `int`. – Gerhardh Dec 08 '20 at 08:26
  • The typecast to `malloc` is needless. – WedaPashi Dec 08 '20 at 08:27
  • Remove `fflush()`. Add the space mentioned before `%c`. Replace `getchar()` with `scanf(" %c",&ch);`. – Weather Vane Dec 08 '20 at 09:13
  • Suggest taking all user-input with `fgets()` and then using `sscanf()` to parse values. That way you de-couple the read and conversion. You will read an entire input with each call to `fgets()` and no matter what happens with the conversion it cannot leave characters unread in your input-stream.. Your problem was the `'\n'` left after reading the integer value and then the `'\n'` being taken as input for your `"%c"` conversion specifier. That's just the tip of the iceberg of pitfalls in using `scanf()`. (Ye have been warned...) – David C. Rankin Dec 08 '20 at 09:19
  • Duplicate [Validating a character input in C](https://stackoverflow.com/q/64673688/3422102) – David C. Rankin Dec 08 '20 at 09:28

1 Answers1

2

Remove these unnecessary spaces in scanf("%c %d %d ",&(a[i].name),&(a[i].start),&(a[i].finish)); and remember to free the memory allocated.

I just modified your code where required, but don't understand the purpose you are reading the last character, since you are not repeating any Menu or something.

anyway this below version works just fine.

#include<stdio.h>
#include<stdlib.h>

 typedef struct
 {
     char name;
     int start;
     int finish;
 }activity;

 int main()
 {
    int n;
    char ch;
    int s,f;

    printf("Enter number of activities: ");
    scanf("%d",&n);
    
    activity* a = (activity*)malloc(sizeof(activity)*n);
    
    for(int i = 0; i < n; i++)
    {
        printf("enter activity details for %d:", i+1);
        scanf(" %c%d%d",&(a[i].name),&(a[i].start),&(a[i].finish));
    }
    
    free(a);
    a = NULL;
    
    printf("Enter activity selected:");
    scanf(" %c",&ch);

    printf("hello");
    return 0;
}

output:

Enter number of activities: 3                                                                                           
enter activity details for 1:A 1 2                                                                                      
enter activity details for 2:B 3 4                                                                                      
enter activity details for 3:C 5 6                                                                                      
Enter activity selected:E                                                                                               
hello 
IrAM
  • 1,720
  • 5
  • 18