user4630535
user4630535

Reputation: 9

Error Handling in C (Files)

I'm currently doing my internal assessment for Computer Science and I've come across a brick wall. I want to search an array for a code which will be inputted by the user. I've already implemented how to search the array for the string but I cant get how to prompt the user to reenter a code if it isnt within the array. Any ideas? Here is my progress How do i prompt the user to enter a code that is stored within an array? that is,if an incorrect code was entered firs

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

Upvotes: 0

Views: 45

Answers (1)

qZanity
qZanity

Reputation: 11

Simply wrap all of the above code in while loop, then after your for loop if it isn't found read the input again from STDIN

Upvotes: 1

Related Questions