Мой код не может прочитать строку от пользователя - PullRequest
0 голосов
/ 07 ноября 2019

Пожалуйста, проверьте функцию ввода. Там, по мне, должна быть проблема, которую я не могу решить. Однако я добавил всю программу для большей ясности.

#include <stdio.h>
#include<stdlib.h>
struct Student
{
int r,y;
char name[20];
char b[10];
char c[10];
};
void input(struct Student *p,int);
void year(struct Student *p,int);
void roll(struct Student *p,int);
int main(void) 
{
typedef struct Student s;
s *p;
int f,n;
printf("Enter the number of Students: ");
scanf("%d",&n);
p=(s *) malloc(n*sizeof(s));
input(p,n);
printf("Enter the 1 to search by year and 2 to search by roll number: ");
scanf("%d",&f);
if(f==1)
{
year(p,n);
}
else if(f==2)
{
roll(p,n);
}
else
{
printf("Invalid Input!");
exit (0);
}
free(p);
return 0;
}
void input(struct Student *p,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter the details of student %d\nEnter the name of student: ",i+1);
scanf("%[^\n]s",(p+i)->name);
printf("Enter the roll number of the student: ");
scanf("%d",&(p+i)->r);
printf("Enter the course of the student: ");
scanf("%[^\n]s",(p+i)->c);
printf("Enter the branch of the student: ");
scanf("%[^\n]s",(p+i)->b);
printf("Enter the year of joining of the student: ");
scanf("%d",&(p+i)->y);
}
}
void year(struct Student *p,int n)
{
int i,y,c=1;
printf("\n\nEnter the year: ");
scanf("%d",&y);
for(i=0;i<n;i++)
{
if((p+i)->y==y)
{
  printf("Student %d :\nName : %s\nRoll number : %d\nCourse : %s\nBranch : %s\n",c++,(p+i)->name,           (p+i)->r,(p+i)->c,(p+i)->b);
 }
 }
 if(c==1)
 {
 printf("Nothing Found!");
}
}
void roll(struct Student *p,int n)
{
int i,r,c=1;
printf("\n\nEnter the roll number: ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
if((p+i)->r==r)
{
  c++;
  printf("Name : %s\nYear of joining : %d\nCourse : %s\nBranch : %s\n",(p+i)->name,(p+i)->y,(p+i)-   >c,(p+i)->b);
 }
 }
 if(c==1)
  {
printf("Nothing Found!");
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...