Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



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

  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<3;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);
         Ask:
         gotoxy(10,4);
         scanf("%d",&employes[i].age);
         /*
         if((arr[employes[i].age])) {
          goto Ask;
         } */
         arr[i]=employes[i].age;

         for(j=i+1;j<3;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<3;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");
       }
      /*
      for(i=0;i<rows;i++)
        {
          for(j=0;j<cols;j++)
            {
               gotoxy(10*j,1+i);
               scanf("%s",employes[i].firstname);
               scanf("%s",employes[i].lastname);
               scanf("%d",&employes[i].age);
               scanf("%d",&employes[i].salary);
               scanf("%d",&employes[i].overtime);
               scanf("%d",&employes[i].dedcution);

            }

        }
        */
      getch();
     return 0;
   }
Posted
Comments
OriginalGriff 17-Oct-14 6:03am    
I'm not even starting to look at that: Get rid of the labels and gotos and I might.
There is no good reason for a beginner to use goto - heck, outside assembly code I haven't used one for twenty years - so it's a bad idea to start off with them.
Rework your code to avoid them (and that's not difficult) and it'll be cleaner and easier to read.

Then explain what you have tried, and what help you need.

1 solution

All the weird goto statements apart, you could try something like this.

C++
const int ArrayLength = 3;  // It is a good practice to use constants instead of 
                            // numeric values all over the code

bool bJump = false;
int iAge = 0;

C++
:Ask
gotoxy(10,4);
scanf("%d",&iAge);
bJump = false;
for (int z=0; z < ArrayLength; z++)
{
    if (iAge == employes[z].age)
    {
        bJump = true;
        break;
    }
}
if (bJump)
    goto Ask;

employes[i].age = iAge;


It is also a bit weird to have a constraint on age. Many people can have the same age.
 
Share this answer
 
Comments
TheSniper105 17-Oct-14 9:21am    
C doesn't have any built in boolean types so how you use bool bJump = false??
George Jonsson 18-Oct-14 9:48am    
If that is your biggest concern, you can check this link using-boolean-values-in-c[^]
TheSniper105 18-Oct-14 10:01am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900