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

标签 c numbers long-integer

我正在创建一个 C 程序作为我今年在学校的最后一个项目,他们想让我创建一个学生数据库程序。就是这样,几乎可以完美运行,但无法正确注册 ID 号。

/image/Ht4Y8.png

在这张图片上,您可以清楚地看到我输入了 23915741843 作为 ID 号,但它自己注册为; -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;
}

最佳答案

TL;DR --> 请使用适当的格式说明符,并始终检查所使用的数据类型可以容纳的值的限制。

在您的代码中,

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

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

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

此外,23915741843 的值太大,无法由 long int 保存。来自 c99

§5.2.4.2.1

maximum value for an object of type long int

LONG_MAX +2147483647 // 231 - 1

您可以使用long long int [%lld]。

旁注:您从未检查过 fopen()scanf() 等是否成功。

关于c - 为什么我无法正确存储/检索 11 位长号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706566/

相关文章:

java - 如何求 mod 的逆

python - 如何强制 Python JSON 输出从多头中排除 'L'

java - long 的 Java 按位表示会有所不同吗?

c++ - 在 C++ 中从 uint8_t 转换为 long

c - libVLC C 库 : Disable generation of album art cache programmatically

c - 使用cachegrind和callgrind的不同读写计数

c - 初学者 : reversing a string with pointers

c - 指向结构体引用的指针数组

javascript - 仅使用 Y 数小于 X 的所有可能性?

java - 设置复系数数组,避免前导零