-1

如果我的帐户 [MAX] 的 MAX 为 10,并且循环提示用户在帐户 [MAX] 中输入帐户#s,我该如何编写代码以查看帐户数量是否超过 10,并告诉用户已输入最大帐户数且不再接受输入?

这是我的 do while 循环

do
{
printf ("Options Available: \n");
printf ("\n 1 - Enter a transaction");
printf ("\n 2 - View the general journal");
printf ("\n 3 - View the balance sheet");
printf ("\n q - Quit the program\n");

printf ("\nPlease enter 1, 2, 3 or q: ");

    option = validateoption();

    if (option == '1')
    {
        printf ("\nEnter an account number (between 1000 and 3999): ");
        accounts[i] = validateaccount();
        printf ("\n");

        printf ("Enter d (debit) or c (credit): ");
        debcred[i] = validatedebcred();
        printf ("\n");

        printf ("Enter transaction amount: ");
        amount[i] = validateamount();
        printf ("\n");

        printf ("\n");

        i++;
        totalinput++;
    }

    if (option == '2')
        journal(accounts, debcred, amount, &totalinput);

    if (option == '3')
        balancesheet(accounts, debcred, amount, &totalinput);

} while (option != 'q');

如果您输入 1、2、3 或 q,就可以说它有效。现在在帐户[i] 中,如果帐户数超过 10,我可以写什么来告诉用户已输入最大帐户数并且不接受更多输入?

我的验证帐户功能:

long validateaccount() {  // VALIDATE INPUT FOR ACCOUNT # IN TRANSACTION FUNCTION

int keeptrying = 1, rc;
 long i;
char after;

do
{
    rc = scanf ("%ld%c", &i, &after);

    if (rc == 0)
    {
        printf (" **Invalid input try again: ");
        clear();
    }
        else if (after != '\n')
        {
            printf (" **Trailing characters try again: ");
            clear();
        }
            else if (i < 1000 || i > 3999)
            {
                printf (" **Invalid input try again: ");
            }
                else
                {
                    keeptrying = 0;
                }

} while (keeptrying == 1);

return i;

}
4

1 回答 1

1

代替

if (option == '1')

if (option == '1' && totalinput < MAX)

当达到最大数量时,选项 1 将被禁用。

printf()您可能还想更早地添加检查,因此如果选项 1 已被禁用,则不要选择它。

注意:您同时拥有itotalinput。我认为如果您删除i并仅使用totalinput. (因为totalinputs比 更具描述性i

于 2012-11-19T23:22:36.867 回答