主程序调试中的 clrscr 错误

clrscr error in main program debug

我在 clrscr(); 的主函数中收到错误,但我想我在使用 fflush(stdin); 时必须清除?

我觉得我在这里遗漏了一些简单的东西,但如果有人能像我一样摆脱一些东西,我将不胜感激!

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

struct person
{
        char name[10];
        int age;
};
typedef struct person NAME;
NAME  stud[10], temp[10];

void main()
{
     int no,i;

     void sort(int N);  /* Function declaration */

     clrscr();
     fflush(stdin);

     printf("Enter the number of students in the list\n");
     scanf("%d",&no);

     for(i = 0; i < no; i++)
     {
         printf("\nEnter the name of  person %d : ", i+1);
         fflush(stdin);
         gets(stud[i].name);

         printf("Enter the age of %d : ", i+1);
         scanf("%d",&stud[i].age);
         temp[i] = stud[i];
     }

     printf("\n*****************************\n");
     printf ("     Names before sorting     \n");
     /* Print the list of names before sorting */
     for(i=0;i<no;i++)
     {
            printf("%-10s\t%3d\n",temp[i].name,temp[i].age);
     }

     sort(no);       /* Function call */

     printf("\n*****************************\n");
     printf ("     Names after sorting     \n");
     printf("\n*****************************\n");
     /* Display the sorted names */
     for(i=0;i<no;i++)
     {
            printf("%-10s\t%3d\n",stud[i].name,stud[i].age);

     }
     printf("\n*****************************\n");
}          /* End of main() */

/* Function to sort the given names */
void sort(int N)
{
         int i,j;
         NAME temp;

         for(i = 0; i < N-1;i++)
         {
                for(j = i+1; j < N; j++)
                {
                    if(strcmp(stud[i].name,stud[j].name) > 0 )
                    {
                        temp    = stud[i];
                        stud[i] = stud[j];
                        stud[j] = temp;
                    }
                }
         }
}       /* end of sort() */
  1. 将函数原型void sort(int N);放在main()
  2. 外面
  3. 您没有(但可以)在 fflush(stdin) 之前执行 clrscr()。在这种情况下,您的屏幕内容(您想清除)与 stdin.
  4. 无关

您可以阅读更多关于 fflush() 和使用它的动机,here

我假设您遇到了编译错误。这是由您看到错误的行上方的行引起的。

正如@Catalyst 所建议的,这是由行

引起的
void sort(int N);  /* Function declaration */`

因为 C 不允许在其他函数内部声明函数(并且 main 一个函数)。

你可以简单地修复它:

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

struct person
{
        char name[10];
        int age;
};
typedef struct person NAME;
NAME  stud[10], temp[10];

void sort(int N);  /* Function declaration */


int main()    // void main is incorrect
{
     int no,i;

     clrscr();
     fflush(stdin);
...

另请注意 int main() 而不是 void main()。它对 Windows 无害,但仍然不正确。