#include <stdio.h>

int i,type;
int buffer[100],line[50];

struct  student
{
    char name[20];
    int age;
    int id;
};
struct student students[3];
    
int main()
{
    for(i=0;i<3;i++)
	{ 
	printf("please enter your name:\n");
    	fgets(students[i].name,sizeof(students[i].name),stdin);
 	printf("please enter your age:\n");
    	fgets(buffer,sizeof(buffer),stdin);
    	sscanf(buffer,"%d",&students[i].age); 
     	printf("please enter your id:\n");
    	fgets(buffer,sizeof(buffer),stdin);
    	sscanf(buffer,"%d",&students[i].id);
	}	
	while(1)
	{
  	int *ptr=students;
     	printf("Please enter the # for the program you want to run:\n");
  	fgets(line,sizeof(line),stdin);
  	sscanf(line,"%d",&type);
	if(type==5)
	break;
  	switch(type)
  	{
        int insertsort(int list[],*ptr) /*function to sort an array of  integers */
                         {
                      	int j, k, index; 
      		or (j=1;j<students[i-1];j++)
           		{ 
			index=list[j]; 
			k=j; 
			while ((k>0) && (list[k-1]>index)) 
			{ 
			      list[k] = list[k-1];
			      k=k-1; 
			} 
			list[k] = index; 
		} 
	} 
	case 1:/* sort by name */
				
  	case 2:/* sort by age  */
  		insertsort(students_ptr->age);
            	case 3:/* sort by id   */
     	        	insertsort(*ptr[i].id);
     	        	
             case 4:
     printf("1-sort by name\n2-sort by age\n3-sort by id\n4-help\n5-Quit\n");
	continue;

             default:
		printf("enter 4 for help\n");
	             continue;
	}
             prinf("idk\n");
} 					 		    
return(0);    
}

as u can see the structure was created so that th euser inputs the students name, age and id. It seems to work fine but i get an error (using dev-c++):21[Warning] passing arg 1 of `fgets' from incompatible pointer type for like every line in that code. Next theres the function, should this be in my while loop, or outside of it? im guessing outside although its not like that in the example. Im kinda lost on the whole bossing of a structure to a function. just some tips would be nice, thanks a bunch!

>passing arg 1 of `fgets' from incompatible pointer type
fgets expects a pointer to char. You pass it a pointer to int because buffer is defined as an array of int. Change buffer to be an array of char and the warning will go away.

>should this be in my while loop, or outside of it?
Functions can only be defined at the top lexical level of a program. This means that your function must be outside of main, which is most certainly outside of the loop. :)

This article has been dead for over six months. Start a new discussion instead.