1

My code is

//header files

#include <stdio.h>
#include<conio.h>

//declare structure for process

struct process
{
    char pr;                //process name
    int A,ex,end,period;   
};

struct process P[10];
int main()
{
    int N,i;//no. of process
    printf("enter number of task \n");
    scanf("%d",&N);
    printf(" enter processname executiontime period ");

    for(i=0;i<N;i++) 
    { 
    printf("\n  P[%d] . process name=",i);
    scanf("%c", &P[i].pr);
    printf("\n  P[%d] . execution time =",i);
    scanf("%d", &P[i].ex);
    printf("\n  P[%d] . period=",i);
    scanf("%d", &P[i].period);
    P[i].A=0;
    P[i].end = P[i].A + P[i].ex;    //here A=0
}
printf("processname\texecutiontime\tperiod\n");
for(i=0;i<N;i++) 
{
    printf("%c\t%d\t%d\n", P[i].pr,P[i].ex,P[i].period);
}
getch();

}

The output i am getting is :

enter number of task
3
 enter processname executiontime period

  P[0] . process name=
  P[0] . execution time =1
  P[0] . period=2
  P[1] . process name=
  P[1] . execution time =3
  P[1] . period=4
  P[2] . process name=
  P[2] . execution time =5
  P[2] . period=6

processname     executiontime   period
    1       2
    3       4
    5       6

What's going on — why won't it wait for the process name?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

This is the input buffer issue. A simple solution for this is to clear the buffer before it you taking the name of process. An easy way is write scanf() like this:

scanf(" %c", &P[i].pr);

Here I wrote a space before " %c"; it will solve your problem. Or use getchar() or fflush(stdin) or int c; while ((c = getchar()) != EOF && c != '\n'); — these are simple solutions for this. Check this code; it will help you,

#include <stdio.h>
//declare structure for process
struct process
{
        char pr;                //process name
        int A,ex,end,period;
};

struct process P[10];
int main()
{
        int N,i;//no. of process
        printf("enter number of task \n");
        scanf("%d",&N);
        printf(" enter processname executiontime period ");

        for(i=0;i<N;i++)
        {
                printf("\n  P[%d] . process name=",i);
                scanf(" %c", &P[i].pr);
                printf("\n  P[%d] . execution time =",i);
                scanf("%d", &P[i].ex);
                printf("\n  P[%d] . period=",i);
                scanf("%d", &P[i].period);
                P[i].A=0;
                P[i].end = P[i].A + P[i].ex;    //here A=0
        }
        printf("processname\texecutiontime\tperiod\n");
        for(i=0;i<N;i++)
        {
                printf("%c\t%d\t%d\n", P[i].pr,P[i].ex,P[i].period);
        }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
suraj
  • 403
  • 4
  • 15
0

Give a space before %c in scanf. This will solve the problem. Replace

scanf("%c", &P[i].pr);

with

scanf(" %c", &P[i].pr);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alex_ban
  • 221
  • 2
  • 11