2015-03-31 42 views
0

我目前正在做我的計算機科學的內部評估,我遇到了一堵磚牆。我想在數組中搜索將由用戶輸入的代碼。我已經實現瞭如何在字符串中搜索數組,但是我無法得到如何提示用戶重新輸入一個代碼,如果它不在數組中。有任何想法嗎?這是我的進展 我如何提示用戶輸入存儲在數組中的代碼?也就是說,如果一個不正確的代碼輸入了冷杉錯誤處理在C(文件)

for (j = 0; j < num_items; j++)  //Loop num_items times   
{     //Beginning of for loop 
    if (strcmp (array[j].code1, codenew) == 0)  //If code is found 
     {    //Beginning of if statement 
     price[i] = item_qty[i] * array[j].price1;  //Calculating the ice f an item 
     printf ("Price : %d", price[i]);  //Prints price 
     printf ("\nEnter '%s' to confirm: ", array[j].itemname1);  /Confirming the item 
     scanf ("%s", item_name[i]); //Reads item name into an array 
     while (strcmp (item_name1[i], array[j].itemname1) != 0)  /Looping until both item names are the same 
     {   //Begin while loop 
     printf ("Incorrect input. Enter '%s' ,Try Again!: ", array[j].itemname1); //Prompt user to try again 
     scanf ("%s", item_name[i]);  //Reads item name into an array       
     }   //End while loop 
     total_price += price[i];  //Calculation of total price 

       //Terminates loop 
     }    //End of if statement 
}     //End of for loop 
+1

什麼是讀取輸入你的問題?我的意思是,除了「我不明白」? – 2015-04-01 00:05:01

+0

@LightningReadstheObituaries我如何提示用戶輸入存儲在數組中的代碼?也就是說,如果首先輸入了不正確的代碼 – user4630535 2015-04-01 00:21:53

+0

這個'while'循環在包含它的'for'循環內循環迭代的時間是多少,當它內沒有增量'i'時,'j'只增加了'for '? 'while(strcmp(item_name1 [i],array [j] .itemname1)!= 0)'?用戶是否被迫不斷重新輸入數據直到其與原始數據匹配? – 2015-04-01 00:39:35

回答

1

簡單的包裝都在while循環上面的代碼,那麼你的for循環後,如果沒有找到再從標準輸入