如何实现错误检查,直到输入文件中的代码为止?这就是我到目前为止:我已经将文件中的每个代码存储在一个名为array[j].code1
的数组中。
Here:
printf("Enter item code: "); //Prompts user
scanf ("%14s", codenew1); //Read user input
len = strlen(codenew1); //Read each character into variable len
while (len != strspn(codenew1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))
{
printf ("Name contains non-alphabet characters. Try again!: "); //Prompts user to try again
scanf ("%14s", codenew1); //Reads user input
len = strlen(codenew1); //Read each character into variable len
} //Endwhile
strncpy(codenew, codenew1,2); //Copy the first two characters from the variable codenew1
codenew[2] = 0; //Store first two characters into variavle codenew
for ( j = 0; j < num_items; j++) //Loop for num_items times
{ //Beginning of for loop
if (strcmp(array[j].code1, codenew) == 0) //If codenew is found in file
{ //Beginning of if statement
price[i] = item_qty[i] * array[j].price1; //Calculating the price of an item
printf("Price : %d", price[i]); //Prints price
printf("\nEnter '%s' to confirm: ", array[j].itemname1); //Confirming the item
scanf("%19s", item_name1[i]);
while (strcmp(item_name1[i], array[j].itemname1 )!=0) //Looping until both item names are the same
{ //Begin while loop
printf("Item name is not %s ,Try Again!: ", array[j].itemname1); //Prompt user to try again
scanf ("%19s", item_name1[i]); //Reads item name into an array
len = strlen(item_name1[i]); //Reads each character into variable len
} //End while loop
len = strlen(item_name1[i]); //Read each character into variable len
while (len != strspn(item_name1[i], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) //While len contains non alphabetic characters
{ //Beginning while
printf ("Name contains non-alphabet characters. Try again!: "); //Prompts user to try again
scanf ("%19s", item_name1[i]); //Read user input
len = strlen(item_name1[i]); //Read each character into variable len
} //End while
strncpy(item_name[i], item_name1[i], 20); //Copy the first two characters from the variable codenew1
item_name[i][20] = 0; //Store first 20 characters in variable item_name[i]
total_price+= price[i]; //Calculate total price
break; //Terminates loop
} //End of if statement
else
if (strcmp(array[j].code1, codenew) != 0) //If codenew is found in file
{
printf("Invalid input! Try again.");
goto Here;
}
} //End of for loop
答案 0 :(得分:0)
代码的当前结构是
Here:
// get "codenew" from user
for ( j = 0; j < num_items; j++ )
{
if ( strcmp(array[j].code1, codenew) == 0) )
{
// do a whole bunch of stuff
break;
}
else if ( strcmp(array[j].code1, codenew != 0) )
{
printf( "Invalid input! Try again.\n" );
goto Here;
}
}
代码的行为是检查代码列表中的第一个条目。如果第一个条目与用户输入匹配,那么“整堆东西”将被执行并且循环中断。否则,将打印一条错误消息,代码将跳回标签Here:
。因此,用户被迫输入列表中的第一个代码。任何其他代码都会跳回到开头。
我假设代码应该做的是扫描整个列表以查看它是否可以找到匹配的条目。为了实现这一点,我会将代码放入一个子程序中,该子程序搜索列表并在找到匹配时返回1
,如果找不到匹配则返回0
。在用户输入有效代码之前,将重复调用该函数。这是拟议函数的结构。
int findCode( void )
{
// get "codenew" from user
for ( j = 0; j < num_items; j++ )
{
if ( strcmp(array[j].code1, codenew) == 0) )
{
// do a whole bunch of stuff
return 1; // SUCCESS, the user entered a valid code
}
}
return 0; // FAILED, no matching code in the list
}
这是函数的调用方式。 while
循环将继续调用函数,直到它返回“success”。
while ( !findCode() )
printf( "Invalid input! Try again.\n" );