0

I am receiving the error in function main for my clrscr(); but I thought I had to clear when using fflush(stdin);?

I feel like I am missing something simple here but if anyone can shed some like I would appreciate it!

#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() */
user311607
  • 69
  • 9
  • 2
    What error are you getting? – Arun A S May 04 '15 at 18:09
  • Your function prototype in the main function should be above main, I'm referring to `void sort(int N);` – Catalyst May 04 '15 at 18:12
  • per the standard, fflush(stdin) is undefined behaviour (even if some specific implementation allows it) Flushing stdin is performed by the loop: while( getchar() != EOF){;} – user3629249 May 04 '15 at 20:26
  • when the function prototype is buried within a function, the when the compiler encounters the actual function, it will assume the default types for parameters and return type (last I checked, that was all 'int') which probably is not what you want. Suggest placing function prototypes after the #include header files and before any actual function declarations – user3629249 May 04 '15 at 20:29
  • the conio.h file is not portable. strongly suggest not using it. use 'system( "cls" );' instead of clrscr(). – user3629249 May 04 '15 at 20:33
  • there are a number of problems with the code: 1) main is declared as 'int main( void ) or 'int main( int argc, char* argv[]) any other form is invalid 2) the definition of 'temp inside the sort() function shadows the global definition of temp, so the actual data will not be sorted. – user3629249 May 04 '15 at 20:37
  • do not typedef struct definitions. the typedef'ing clutters the code, makes it harder for humans to read, often is mis-leading, and clutters the compiler name space. So everywhere in the code that 'NAME' is used, replace with 'struct person' – user3629249 May 04 '15 at 20:38
  • always check (not the parameters)the returned value from scanf(), and family of function, to assure the operation was successful. – user3629249 May 04 '15 at 20:41
  • do not use 'gets()' as it is removed from the language standard, is depreciated, is full of security holes, can easily overrun the input buffer. Suggest using: 'fgets()' – user3629249 May 04 '15 at 20:42
  • @user3629249 : as OP uses conio.h, I assume the program is compiled with MSVC which supports using fflush to cleanup input streams even if it is not standard. – Serge Ballesta May 04 '15 at 20:45
  • for robust code, after inputting the 'age', check that it is a positive number and check for a reasonable upper limits, for instance 100. – user3629249 May 04 '15 at 20:46
  • this line: 'temp[i] = stud[i];' will not copy the struct entry. Suggest: memcpy( &temp[i], &stud[i], sizeof(struct person) ); – user3629249 May 04 '15 at 20:49
  • this line: 'gets(stud[i].name);' will input a string (any length)(including the newline), append a '\0' at the end. So, on DOS/Windows, the max usable name (without buffer overflow) is 7 characters. on other OSs, the max usable name (without buffer overflow) is 8 characters. Is 7 or 8 characters enough? my name: 'Richard', would have good chance of overflowing the buffer. Also, when displaying/sorting, etc the array of struct person, you (probably) do not want to be sorting a newline. Therefore, 1) make the field significantly longer, 2) replace the newline with a '\0' character – user3629249 May 04 '15 at 20:55
  • the sort() function will fail because (for instance) temp = stud[i] will not copy the whole struct. suggest using: memcpy( &temp, &stud[i], sizeof(struct person) ); – user3629249 May 04 '15 at 21:00
  • please use spaces when indenting (I like 4 spaces as that shows nicely, even with variable width fonts) because every decent editor/wordprocessor can/will set the tab width or tab stops differently – user3629249 May 04 '15 at 21:02

2 Answers2

2
  1. Put the function prototype void sort(int N); outside main()
  2. You don't have (but you may) execute clrscr() before fflush(stdin). In this case contents of your screen (which you want to clear) have nothing to do with stdin.

You can read more about fflush() and the motivation to use it, here.

Community
  • 1
  • 1
syntagma
  • 23,346
  • 16
  • 78
  • 134
2

I assume that you get a compilation error. It is caused by the line above the one where you see the error.

As suggested by @Catalyst, it is caused here by the line

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

because C does not allow functions to be declared locally inside other functions (and main is a function).

You can simply fix it that way :

#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);
...

Note also the int main() instead of void main(). It is harmless on Windows, but is still incorrect.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Ok I moved it that was dumb haha. Still getting the same error. clrscr(); is highlighted with in function mainas the error. – user311607 May 04 '15 at 19:00