該程序的邏輯非常清晰,但它要求用戶輸入名稱。它第二次要求這個名字,即在i = 1時,它要求輸入名字,並要求輸入年份。總之它不允許用戶在int年的i = 0之後輸入數據。程序給出錯誤
/* 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
對不起,我的程序有一些錯誤。請不要使用fflush(stdin)。有關更多信息,請參閱http://www.drpaulcarter.com/cs/common-c-errors.php。 – chanchal1987 2010-07-20 12:42:14