c - C 中的错误处理(文件)

标签 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

最佳答案

只需将上述所有代码包装在 while 循环中,然后在 for 循环之后,如果找不到,则再次从 STDIN 读取输入

关于c - C 中的错误处理(文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380554/

相关文章:

c - stdint.h 和 C99

c++ - 避免模​​ p 运算溢出

c - 使用 bash 脚本 fork exec 和管道

c - 如何设计我的宏以包含函数返回值

c - 在什么版本的 C 中,括号内的 block 用于返回有效值?

c - C语言中变量声明和错误处理优先级的最佳实践

c - 为什么这个 qsort() 不起作用?

C: 为什么我在 free() 上会出错

python - 用于在磁盘上有效存储整数对集的数据结构选项?

c++ - 我可以使用 ICC 为多种 CPU 架构进行编译吗?