3 Replies - 328 Views - Last Post: 03 November 2012 - 03:21 AM Rate Topic: -----

#1 ali11  Icon User is online

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 50
  • Joined: 25-October 11

can somebody help me to fix my errors

Posted 02 November 2012 - 07:57 PM

//************************************
//Includes


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


//************************************
//Globals
int packed;


//************************************
//Function Prototypes


int pack_value(int value, int size, int low_bit);
int unpack_value(int holder, int size, int low_bit);
void get_date(int holder, int day, int month, int year);
void get_permissions(int permission, int clas, int *read , int *write , int *execute);
int w_permission(int clas, int *read , int *write , int *execute);
int take_permissions(int clas,int *r, int *w, int *e);




//***********************************
//Main function

int main(int argc, char** argv) 
{

    //Variables


	int d,m,y;			//date variables
	
	int r,w,e;			//permission variables
	
	int p1,p2,p3;		//packed class variables
	
	int i;
	int permissions;
	int day, month, year;
	char a;

   

	
	printf("DATE:\n");
  

	//getting day and packing it
	printf(" Day:"); 
    scanf("%d",&day);
    d = pack_value(day,31, 0);   //31 = 5 one's on the lower bits

  

	//getting month and packing it
	printf(" Month:");
    scanf("%d",&month);
    m=pack_value(month,15, 5);   //15 = 4 one's on the lower bits

  

	//getting Year and packing it
	printf(" Year:"); 
    scanf("%d",&year);
    y=pack_value(year,4095, 10); //4095 = 12 one's on the lower bits



	//Packing date in one variable
	packed = d|m|y;


  

	//Taking permissins
	printf("\n\nPermissions for Owner :\n");
	p1=take_permissions(1,&r,&w,&e);




	printf("\nPermissions for Group :");
	p2=take_permissions(2,&r,&w,&e);



	printf("\nPermissions for Others :");
	p3=take_permissions(3,&r,&w,&e);


 
 
	//packing permissions
	permissions=p1|p2|p3;
  
  
	//Unpacking date
	get_date( packed, &d, &m, &y);
    
	//Printing Date
	printf("\n\nDATE\n day:%d\n month:%d\n year:%d\n", d, m, y);


//*****
//Unpacking permissins and printing them


	for(i=1;i<=3;i++)
	{
		
		switch(i)
		{


		case 1:
			printf("\nPermissions for Owner :");
			
			get_permissions(permissions,i,&r,&w,&e);
		
				
			  if(r==1)
				
				printf("read " );
			  if(w==1)
				printf("Write " );
			  if(e==1)
				  printf("Execute " );
			  
			  break;
		
		
		case 2:
			
			printf("\nPermissions for Group :");
			
			get_permissions(permissions,i,&r,&w,&e);
		
				
			  if(r==1)
				
				printf("read " );
			  if(w==1)
				printf("Write " );
			  if(e==1)
				  printf("Execute " );
			  
			  break;
				  
		
		
		case 3:
			
			printf("\nPermissions for Others :");
			
			get_permissions(permissions,i,&r,&w,&e);
		
				
			  if(r==1)
				
				
				  printf("read " );
			  if(w==1)
				
				  printf("Write " );
			  if(e==1)
				  
				  printf("Execute " );
			  
			  break;
				
		
		

		}

	}
	

//****

printf("\n");
    
	return (EXIT_SUCCESS);
}


//***********************************
//Function to pack and shift value


int pack_value(int value, int size, int low_bit)
{
	
	int bit_check;
	bit_check= value & size;
	if (bit_check == value)
        {value= value << low_bit;
	return value;}
	else
	printf("Bit Error!");
	exit(1);
	
}



//***********************************
//Function Unpack value
	
int unpack_value(int holder, int size, int low_bit)
{
	int unpack;
	unpack= (holder>>low_bit)&size;
	return unpack;
	
}



//***********************************
//Function to get date
	
void get_date(int holder, int *day, int *month, int *year)
{
          //int packed_value= day|month|year;
	
		*day= unpack_value(holder, 31, 0);
	
		*month= unpack_value(holder, 15, 5);
	
		*year= unpack_value(holder, 4095, 10);

		//printf("DATE\n day:%d\n month:%d\n year:%d", day, month, year);
}




//***********************************
//Function unpack date

void get_permissions(int permission, int clas, int *read , int *write , int *execute)
{

	
	//(Class*3) will determine how much the shift is needed
	
	*read= unpack_value(permission, 1, (clas*3)-3);
	
	*write= unpack_value(permission, 1, ((clas*3)-2));
	
	*execute= unpack_value(permission, 1,((clas*3)-1));





}



//***********************************
//Function to write permissions

int w_permission(int clas, int *read , int *write , int *execute)
{

	
	int p1,p2,p3,per=0;
	
	p1	= pack_value(*read, 1, (clas*3)-3);
	
	p2	= pack_value(*write, 1, ((clas*3)-2));
	
	p3	= pack_value(*execute, 1,((clas*3)-1));

	per =p1|p2|p3;

return per;

}


//***********************************
//Function to take permissions from user

int take_permissions(int clas,int *r, int *w, int *e)
{


	int p1;
	char a;

	printf("\nread  (y/n):"); 
  
	a=getch();
  
	if(a=='y')
	{
	
		*r=1;
  
	}else *r=0;

   
  
	printf("\nwrite  (y/n):"); 
  
  
 
	a=getch();
 
 
	if(a=='y')
	{
	
		*w=1;
  
	}else *w=0;

	printf("\nexecute  (y/n):"); 
  
	a=getch();
	if(a=='y')
	{
	
		*e=1;
  
	}else *e=0;


  
	p1=w_permission(clas,r,w,e);



 return p1;

}


prog.c: In function ‘main’:
prog.c:106: warning: passing argument 2 of ‘get_date’ makes integer from pointer without a cast
prog.c:106: warning: passing argument 3 of ‘get_date’ makes integer from pointer without a cast
prog.c:106: warning: passing argument 4 of ‘get_date’ makes integer from pointer without a cast
prog.c:48: warning: unused variable ‘a’
prog.c:58: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:65: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:72: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c: At top level:
prog.c:231: error: conflicting types for ‘get_date’
prog.c:22: error: previous declaration of ‘get_date’ was here
prog.c: In function ‘take_permissions’:
prog.c:304: warning: implicit declaration of function ‘getch’

This post has been edited by JackOfAllTrades: 03 November 2012 - 02:46 AM
Reason for edit:: Separated errors from code


Is This A Good Question/Topic? 0
  • +

Replies To: can somebody help me to fix my errors

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: can somebody help me to fix my errors

Posted 03 November 2012 - 01:12 AM

What errors are you getting?

This post has been edited by aresh: 03 November 2012 - 03:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: can somebody help me to fix my errors

Posted 03 November 2012 - 02:43 AM

Note: The OP has posted the errors with the code in the end, scroll down to see...

@OP, There is a conflict between the functions declaration, definition and the arguments you are passing to them... Just review your code, and its clearly figured out in the errors you have got...

prog.c:231: error: conflicting types for ‘get_date’
// In declaration, all the parameters are of type int
void get_date(int holder, int day, int month, int year);

//When called, you passed addresses
get_date( packed, &d, &m, &y);

// And in the definition, the function signature is totally changed
void get_date(int holder, int *day, int *month, int *year)
{

}



Hope this Helps!
Was This Post Helpful? 2
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: can somebody help me to fix my errors

Posted 03 November 2012 - 03:21 AM

Actually, when I first saw the code, he had posted the errors with the code, so I could not see them. My mistake.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1