我目前正在对计算机科学进行内部评估,但遇到了障碍。我想在数组中搜索用户将输入的代码。我已经实现了如何在数组中搜索字符串,但我无法了解如何提示用户重新输入代码(如果它不在数组中)。有任何想法吗?这是我的进步 如何提示用户输入存储在数组中的代码?也就是说,如果首先输入了错误的代码
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
最佳答案
只需将上述所有代码包装在 while 循环中,然后在 for 循环之后,如果找不到,则再次从 STDIN 读取输入
关于c - C 中的错误处理(文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380554/