我正在做一個程序,要求用戶輸入姓名和身份證號......然後我添加了一個搜索功能,例如搜索名稱「bob」,它表示「Bob被發現與ID陣列位置x:XXXXX」程序輸出C中的隨機數
我的問題是,‘ID:代碼XXXX’部分是給我的隨機數字......這應該是如1234,但它給我,就像4210832
東西下面是代碼
void search_surname()
{
int index, found=0;
char surname_to_find[10];
printf("Please enter the student surname to search for\n\n");
gets(surname_to_find);
fflush(stdin);
system("cls");
for(index=0;index<height_of_array;index++)
{
if(strcmpi(surname_to_find, surname[index]) == 0)
{
found=1;
system("cls");
printf("%s found in array position %i, with the ID: %i \n\n", surname_to_find,
index+1, id[array_index]);
getch();
我不是100%肯定它爲什麼給我隨機數
謝謝。
編譯時是否啓用了警告? –
'gets'不應該在任何情況下使用。它會打開一個緩衝區溢出漏洞,除非不使用'gets',否則**不能被修復。除此之外,你真的需要縮進你的代碼。 – ThiefMaster
什麼是'id [array_index]'? – dasblinkenlight