我在下面的代碼中有一個問題,請求是選項2。每次我打印出請求表時,不是顯示用戶輸入的待處理請求,而是給我一個隨機數。有人可以幫我找到問題嗎?代碼應該與選項1相似,但如果它的請求相同,它不應該將用戶輸入相加。示例輸出:基本的食品銀行程序幫助需要
1
Milk 20
1
Milk 20
2
Milk 10
2
Milk 5
4
Donations:
Milk 40
Requests:
Milk 10
Milk 5
這是我的代碼
int main() {
int don_count=0, don_amt[100], found, i, don_quant, option, req_count=0, req_amt[100],req_quant;
char word[20], don_inv_type[100][20], req_word[20], req_inv_type[100][20];
printf("Welcome to the food bank program!);
printf("1.Enter a Donation\n2.Enter a Request\n3.Fulfill the earliest Request\n4.Print status report\n5.Exit\n");
scanf("%d", &option);
while (option != 5) {
// Execute a deposit.
if (option == 1) {
scanf ("%s", word);
scanf ("%d", &don_quant);
found = -99;
for (i=0;i<don_count; i++){
if (strcmp(don_inv_type[i], word)==0)
found = i;
}
if (found == -99){
strcpy(don_inv_type[i],word);
don_amt[i] = don_quant;
don_count ++;
}
else
don_amt[found] += don_quant;
}
else if (option == 2) {
scanf ("%s", req_word);
scanf ("%d", &req_quant);
req_count++;
for(i=0; i<req_count; i++)
{
strcpy(req_inv_type[i],req_word);
req_amt[i] += req_quant;
}
}
else if (option == 3) {
}
else if (option == 4) {
printf("Donations:\n");
for(i=0;i<don_count;i++){
printf("%8s", don_inv_type[i]);
printf("%5d\n", don_amt[i]);
}
printf("Requests: \n");
for(i=0;i<req_count;i++){
printf("%8s", req_inv_type[i]);
printf("%5d\n", req_amt[i]);
}
}
else if (option == 5) {
printf(" Thanks bye");
}
// Reprompt the menu.
printf("1.Enter a Donation\n2.Enter a Request\n3.Fulfill the earliest Request\n4.Print status report\n5.Exit\n\n");
scanf("%d", &option);
} // end while
system("PAUSE");
return 0;
} // end main
瞭解如何使用調試器來調試您的程序。 – 2014-10-08 23:51:07
@BryanChen-是的,就像這樣會發生,永遠:(( – 2014-10-08 23:54:02
它的一個問題,說req_amt [i] + = req_quant; – Perez 2014-10-09 00:03:07