user379888
user379888

Reputation:

program giving error

The logic of the program is quite clear but when it asks the user to enter name. The second time it asks for the name i.e at i=1 it asks for the name and also asks for the year to be entered. In short its not allowing to the user to enter data after i=0 in int year.

/* Write a program to take input name roll number and year of joining of 5 students and making a function which prints name of only those who have joined in the particular year mentioned by the user*/
#include<stdio.h>
#include<conio.h>
struct student
{
   char name[50];
   int year;

}
a[5];

void func ( void );
void main ( void )
{
   int i;
   for ( i = 0; i < 5; i++ )
   {
      printf ( "Enter name %d\n", i + 1 );
      gets ( a[i].name );
      puts ( "Enter year" );

      scanf ( "%d", &a[i].year );
   }
   func(); 
   getch();
}
void func ( void )
{
   int i;
   int yearr;
   printf ( "Enter a year:" );
   scanf ( "%d", &yearr );
   for ( i = 0; i < 5; i++ )
   {
      if ( yearr == a[i].year )
      {
         printf ( "%s", a[i].name );
      }// if ends

   }//for ends
}// func ends

Upvotes: 0

Views: 156

Answers (3)

cHao
cHao

Reputation: 86506

Aside from the code stink of gets (USE fgets. PLEASE. NOW, WHILE YOU'RE STILL LEARNING RIGHT FROM WRONG.), and the hideosity of your output (A \n here and there would do wonders), it looks like it could work. Assuming what you want it to get 5 names and years from the user, then ask for a year to search for, and list all students' names whose years match. (If that's not what you want, the logic isn't even nearly as clear as you think it is.)

Personally, i wouldn't be mixing scanf and fgets (Yes, i said fgets. USE IT.), so i'm not sure about problems with doing so. I'm not a fan of scanf anyway, so i may be biased.

Upvotes: 3

chanchal1987
chanchal1987

Reputation: 2357

Clear the input buffer before taking input using fflush(stdin) or fflushall(). Your modified code is given below.

/* Write a program to take input name roll number and year of joining of 5 students and making a function which prints name of only those who have joined in the particular year mentioned by the user*/
    #include<stdio.h>
    #include<conio.h>
    struct student
    {
       char name[50];
       int year;

    }
    a[5];

    void func ( void );
    void main ( void )
    {
       int i;
       for ( i = 0; i < 5; i++ )
       {
          printf ( "Enter name %d\n", i + 1 );
          fflush(stdin);
          gets ( a[i].name );
          puts ( "Enter year" );

          scanf ( "%d", &a[i].year );
       }
       func(); 
       getch();
    }
    void func ( void )
    {
       int i;
       int yearr;
       printf ( "Enter a year:" );
       scanf ( "%d", &yearr );
       for ( i = 0; i < 5; i++ )
       {
          if ( yearr == a[i].year )
          {
             printf ( "%s", a[i].name );
          }// if ends

       }//for ends
    }// func ends

Upvotes: 1

onof
onof

Reputation: 17367

I think your problem is related to this question: Input in C. Scanf before gets. Problem.

Try:

scanf("%d\n", &yearr);

Upvotes: 1

Related Questions