Thread: prevent duplicate age when insert age

  1. #1
    Registered User
    Join Date
    Oct 2014
    Location
    Mansourah, Ad Daqahliyah, Egypt, Egypt
    Posts
    2

    Question prevent duplicate age when insert age

    i'm newbie in c programming langauge


    i created program that insert employes data and then print their data

    but never accept duplicate age if user entered duplicated age promot him to enter another age (age must be unique)


    here is my code


    Code:
    #include<conio.h>
    #include<stdio.h>
    #define size 3
    
    
      struct emp
      {
    	int age,overtime,dedcution,netsal;
    	float salary;
    	char  firstname[15];
    	char  lastname[15];
    
    
      };
      int main()
       {
    	  int i,j,unq=1,arr[3]={0};
    	  float temp=0.0;
    	  struct emp employes[3];
    	  clrscr();
    	  printf("please insert data of employee\n");
    	  for(i=0;i<size;i++)
    	  {
    
    
    
    
    		 clrscr();
    
    
    		 gotoxy(5,2);
    		 printf("firstname");
    		 gotoxy(40,2);
    		 printf("lastname");
    		 gotoxy(5,4);
    		 printf("age");
    		 gotoxy(40,4);
    		 printf("salary");
    		 gotoxy(5,6);
    		 printf("overtime");
    		 gotoxy(40,6);
    		 printf("nset salary");
    		 gotoxy(16,2);
    		 scanf("%s", employes[i].firstname);
    		 gotoxy(50,2);
    		 scanf("%s",employes[i].lastname);
    
    
    		 gotoxy(10,4);
    		 scanf("%d",&employes[i].age);
    
    
    		 arr[0]=employes[0].age;
    		 for(j=i+1;j<size;j++)
    		   {
    			  if(arr[i] == employes[j].age )
    			   {
    					  unq=0;
    					 break;
    			   }
    
    
    		   }
    		   if(unq==0)
    			{
    			   gotoxy(10,4);
    			   scanf("%d",&employes[i].age);
    
    
    			}else{
    			 arr[i]=employes[i+1].age;
    			}
    		 /*
    		 for(j=i+1;j<size;j++)
    		  {
    
    
    			if(arr[i] == employes[j].age)
    			  {
    				unq=0;
    				break;
    			  }
    		  }
    		 if(!unq) unq=1; goto Ask;
    		 */
    		 gotoxy(50,4);
    
    
    		 scanf("%f",&temp);
    		// employes[i].salary=0;
    		 employes[i].salary =temp;
    		 gotoxy(20,6);
    		 scanf("%d",&employes[i].overtime);
    		 gotoxy(55,6);
    		 scanf("%d",&employes[i].netsal);
    
    
    	   }
    	   clrscr();
    	  for(i=0;i<size;i++)
    	   {
    		  printf("first name :\t %s \n",employes[i].firstname);
    		  printf("last name :\t %s \n ",employes[i].lastname);
    		  printf("age:\t %d \n",employes[i].age);
    		  printf("salary:\t %f \n",employes[i].salary);
    		  printf("overtime:\t %d \n",employes[i].overtime);
    		  printf("netsal :\t %d \n",employes[i].netsal);
    		  printf("\n _______________________ \n");
    	   }
    
    
    	  getch();
    	 return 0;
       }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, but that sounds weird: isn't it normal for employees to have the same age (in years)?

    Anyway, what you can do is to search the array for an employee with the given age. If you find such an employee, report the duplicate to the user. Otherwise, proceed as per normal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Location
    Mansourah, Ad Daqahliyah, Egypt, Egypt
    Posts
    2
    i want to insert only one employe from each age not all ages

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    For a small set like age I would just use an array of char

    Code:
    int
    have_seen_age(unsigned int age)
    {
       static char  age_used[128] = { 0 };
       
       if((age > 127 ) || age_used[age]) 
          return(1);
       age_used[age] = 1;
       return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to prevent encapsulation in c++ ?
    By mannu1200 in forum C++ Programming
    Replies: 11
    Last Post: 01-26-2014, 02:17 AM
  2. how do i duplicate this in c++
    By lilhawk2892 in forum C++ Programming
    Replies: 42
    Last Post: 08-16-2006, 08:11 PM
  3. prevent piping?
    By barneygumble742 in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2005, 01:07 AM
  4. prevent exiting
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-12-2001, 02:57 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM

Tags for this Thread