为什么我无法正确存储/检索11位数的长号码

时间:2014-12-30 13:41:56

标签: c numbers long-integer

我创建了一个C程序作为我今年在学校的最后一个项目,他们想让我创建一个学生数据库程序。在这里,工作几乎完美无瑕,但它不会正确注册身份证号码。

http://i.imgur.com/46fCxPc.png

在此图片上,您可以清楚地看到我输入了23915741843作为身份证号码,但它注册为; -1854061933。

我已经用了很长时间来修复它,但是没有。它不会真正起作用。我被允许从互联网上获取帮助。所以这是我的完整代码。它有什么问题?

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

struct
{
    long int id;
    char firstname[20];
    char lastname[20];
    int mark;
}student;

int main()
{
   long int idnumber;
   int flag,choice,shift,found,continu,length;
   char studentname[20];
   FILE *fp;

    printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
   Label1:
      printf("\n1 -> Store a new record in database\n");
      printf("2 -> Search a student record by Student First Name\n");
      printf("3 -> Search a student record by ID\n");
      printf("4 -> Quit Student Database");
      printf("\n\n");
      printf("Enter your choice : ");
      scanf("%d",&choice);
      switch(choice)
      {
       case  1:
       Label2:
       printf("\nEnter Student Details:\n\nID number: ");
       scanf("%d",&student.id);
       printf("\nName:");
       scanf("%s",student.firstname);
       printf("\nSurname:");
       scanf("%s",student.lastname);
       printf("\nMark(0 - 100 integer) : ");
       scanf("%d",&student.mark);
       fp=fopen("studentfile.txt","a+");
       fprintf(fp,"\n%d\t%s\t%s\t%d\t",student.id,student.firstname,student.lastname,student.mark);
       fclose(fp);
       printf("A student record has been added successfully...\n");
       printf("\n\n1 -> Wish to add another record to database");
       printf("\n2 -> Wish to move to Main Menu");
       printf("\n3 -> Exit from Program\n");
       scanf("%d",&shift);
       if(shift==1)
        goto Label2;
       if(shift==2)
        goto Label1;
       if(shift==3)
        break;
       if(shift!=1&&2&&3){
        printf("Exiting.........");
        break;
        }

       case 2:
       Label4:
       printf("\nEnter student first name: ");
       scanf("%s",&studentname);
       printf("Searching record with studentname=%s.\n",studentname);
           found=0;
           if((fp=fopen("studentfile.txt","r"))==NULL)
        {
            printf(" ! The File is Empty...\n\n");
        }
        else
        {
            while(!feof(fp)&& found==0)
                {
                fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
                length = strlen(student.firstname);
                if(student.firstname[length]==studentname[length])
                    found=1;
            }
            }
       if(found)
       {
         printf("\nThe record is found.\n");
         printf("\nID: %d\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
       }
       else
       {
         printf("Not found...\n");
         getch();
       }
       Label5:
       printf("\n\n1 -> Wish to search another record");
       printf("\n2 -> Wish to move to Main Menu");
       printf("\n3 -> Exit from Program\n");
       scanf("%d",&shift);
       if(shift==1)
        goto Label4;
       if(shift==2)
        goto Label1;
       if(shift==3)
        break;
       if(shift!=1&&2&&3){
        printf("\nEnter a valid choice");
        goto Label5;
        }
       case 3: 
       Label6:
       printf("\nEnter the ID: ");
       scanf("%d",&idnumber);
       printf("Searching record with ID=%d.\n",idnumber);
           found=0;
           if((fp=fopen("studentfile.txt","r"))==NULL)
        {
            printf(" ! The File is Empty...\n\n");
        }
        else
        {
            while(!feof(fp)&& found==0)
                {
                fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
                if(student.id==idnumber)
                    found=1;
            }
            }
       if(found)
       {
         printf("\nThe record is found.");
         printf("\nID no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
       }
       else
       {
         printf("Not found...\n");
         getch();
       }
       Label7:
       printf("\n\n1 -> Wish to search more..");
       printf("\n2 -> Wish to move to Main Menu");
       printf("\n3 -> Exit from Program\n");
       scanf("%d",&shift);
       if(shift==1)
        goto Label6;
       if(shift==2)
        goto Label1;
       if(shift==3)
        break;
       if(shift!=1&&2&&3){
        printf("\nEnter a valid choice");
        goto Label7;
        }
       case 4: break;
       default :
          printf(" Bad choice...Enter the choice again...\n");
          goto Label1;
        }

      getch();
      return 0;
}

2 个答案:

答案 0 :(得分:2)

TL; DR - &gt;请使用适当的格式说明符,并始终检查使用的数据类型可以保存的值的限制。

在您的代码中,

scanf("%d",&student.id);

%d不是long int的正确格式说明符。

  • 对于long int,它应为%ld
  • 对于unsigned long int,它应为%lu

此外,23915741843是一个太大而无法被long int占据的值。来自§5.2.4.2.1的{​​{1}}

  

long int

类型的对象的最大值      

LONG_MAX +2147483647 // 2 31 - 1

您可以使用c99 [long long int]。

旁注:您从未检查%lldfopen()等是否成功。

答案 1 :(得分:0)

scanf("%ld",&student.id);

使用正确的格式说明符来扫描long int。 IMO id将是无符号值,因此如果错误,请将id设为unsinged long int id,并使用%lu扫描该值。如果没有,请确保该值符合允许的范围,然后使用long long int并使用%lld格式说明符进行相应扫描。

使用goto时,您的代码看起来很混乱。请确保您已克服它。

显示您的提示并让切换案例相应地处理它。如果用户想要退出时有一个单独的案例并处理这种情况而不是使用goto