Я не могу заставить мой оператор if выполняться правильно - PullRequest
0 голосов
/ 24 марта 2019

Кажется, я не могу правильно выполнить свои операторы if в разделе «Ввод и вывод средств». Он будет работать правильно, когда число отрицательное, но когда оно положительное, оно отображает сообщение об ошибке, а затем завершает цикл и переходит к снятию.

#include <stdio.h>
int main (void)
{
    int deposits = 0, totaldeposits = 0, deposited = 0, totalwithdrawals = 0, withdrawals, d = 0, w = 0;
    float balance = 0, b;

        printf ("Welcome to the computer banking system\n\n");

    for ( b = 0; b <= balance; ++b )
    {
        printf ("Enter your current balance in dollars and cents:");
            scanf ("%f", &b);
            balance = balance + b;

        if ( balance <= -1 )
        {
            printf ("***Beginning balance must be at least zero, please re-enter.\n\n");
                balance = balance - b, --b;

        }/*end of if statement*/
    }/*end of for loop*/

    for ( d = 0; d <= deposits; ++d )
    {       
        printf ("\nEnter the number of deposits (0-5):");
            scanf ("%i", &d);
        deposits = deposits + d;

        if ( deposits > 5 || deposits < 0 )
        {
            printf ("*** Invalid number of deposits, please re-enter.");
                deposits = deposits - d;
        }/*end of if statement for deposits*/       
    }/*end of for loop for deposits*/
    for ( w <= 0; w <= withdrawals; ++w)
    {
        printf ("\n\nEnter the number of withdrawals (0-5):");
            scanf ("%i", &w);
            withdrawals = withdrawals + w, ++w;

        if ( withdrawals > 5 || withdrawals < 0 )
        {
        printf ("***Invalid number of withdrawals, please re-enter.");
        withdrawals = withdrawals - w, --w;
        }/*end of withdrawals if*/
    }/*end of withdrawals for loop*/





getchar;        
return 0;

} /*End main*/

Ответы [ 2 ]

0 голосов
/ 24 марта 2019

Кажется, у вас есть какая-то ошибка в логике вашей программы. Я бы переписал

for ( d = 0; d <= deposits; ++d )
{       
    printf ("\nEnter the number of deposits (0-5):");
        scanf ("%i", &d);
    deposits = deposits + d;

    if ( deposits > 5 || deposits < 0 )
    {
        printf ("*** Invalid number of deposits, please re-enter.");
            deposits = deposits - d;
    }/*end of if statement for deposits*/       
}/*end of for loop for deposits*/

в

do {
    printf ("\nEnter the number of deposits (0-5):");
    scanf ("%i", &deposits);

    if (deposits < 0 || deposits > 5) {
        printf ("*** Invalid number of deposits, please re-enter.");
    } else {
        break;
    }
} while (1);

То же самое можно сделать для других циклов.

Идея состоит в том, чтобы запросить ввод по крайней мере один раз и проверить, является ли введенное значение действительным для вашего случая, если нет, запросите ввод еще раз.

0 голосов
/ 24 марта 2019

Вы используете одну и ту же переменную d в цикле for и для ввода данных. Это вызывает проблему. Используйте следующий код: -

int dep;
for ( d = 0; d <= deposits; ++d )
{       
    printf ("\nEnter the number of deposits (0-5):");
        scanf ("%i", &dep);
    deposits = deposits + dep;

    if ( deposits > 5 || deposits < 0 )
    {
        printf ("*** Invalid number of deposits, please re-enter.");
            deposits = deposits - dep;
    }/*end of if statement for deposits*/  
    else break;     
}/*end o
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...