我正在創建一個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;
}
你不能使用函數嗎?不要使用'goto'。 'goto'是有用的,但不是在這種情況下。如果你不被允許使用功能,那麼沒關係。 –
「學生身份證號碼」實際上是一個恰好包含數字*,而不是*數字*的字符串(您不需要用數學計算)。你可能想把它作爲一個字符串來處理。這在某些情況下會有所幫助(例如,學生ID中的前導'0',允許數字之間的標點或空格...)。 *編輯*:另外,如果有人輸入超過19個字符作爲名稱會發生什麼。 – DCoder
@DCoder沒錯,讓我試着把它變成一個字符串。 – tommythelittleguy