插入年龄时防止重复年龄 [英] prevent duplicate age when insert age

查看:62
本文介绍了插入年龄时防止重复年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c编程语言的新手





i创建的程序插入雇用数据然后打印他们的数据



但如果用户输入重复年龄,则永远不会接受重复年龄促使他进入另一个年龄(年龄必须是唯一的)





这是我的代码







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



#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;
   }

推荐答案

所有奇怪的goto语句除外,你可以试试这样的东西。



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

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;




: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.


这篇关于插入年龄时防止重复年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆