2014-12-30 24 views
2

我正在創建一個C程序作爲我今年在我學校的最後一個項目,他們想讓我創建一個學生數據庫程序。在這裏,工作幾乎完美無瑕,但不會正確註冊身份證號碼。爲什麼我無法正確存儲/檢索11位數字的長號碼

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; 
} 
+1

你不能使用函數嗎?不要使用'goto'。 'goto'是有用的,但不是在這種情況下。如果你不被允許使用功能,那麼沒關係。 –

+0

「學生身份證號碼」實際上是一個恰好包含數字*,而不是*數字*的字符串(您不需要用數學計算)。你可能想把它作爲一個字符串來處理。這在某些情況下會有所幫助(例如,學生ID中的前導'0',允許數字之間的標點或空格...)。 *編輯*:另外,如果有人輸入超過19個字符作爲名稱會發生​​什麼。 – DCoder

+0

@DCoder沒錯,讓我試着把它變成一個字符串。 – tommythelittleguy

回答

2

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類型長整型

LONG_MAX 2147483647的目的// 2 - 1

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

旁註:你從來沒有檢查的fopen()成功,scanf()

+0

我做了很久很久的int,並改變了'scanf(「%d」,&student.id);'帶有'scanf(「%lld」,&student.id)的''部分;'now; 0x60e5984f出現未處理的異常。 (註冊時。) – tommythelittleguy

+0

我只是改變了你想要的代碼。 long long int在第7行和第15行。 scanf在第34行更改。 程序編譯,但每當我試圖註冊一個測試學生。它給了這個錯誤。 – tommythelittleguy

+0

@tommythelittleguy是否檢查過_all_格式說明符? –

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

使用正確的格式說明掃描長整型。 IMO id將是一個無符號值,所以如果這是一個錯誤,使idunsinged long int id和使用%lu來掃描值。請確保該值符合允許的範圍,如果不是,則使用long long int並使用%lld格式說明符進行相應的掃描。

您的代碼看起來使用goto模糊不清,請確保您解決了它。

顯示您的提示,並有切換案例相應地處理它。如果用戶想要退出有一個單獨的案例,並處理這種情況下,而不是使用goto

相關問題