简体   繁体   English

如何调试该基本的IF-ELSE程序?

[英]How can I debug this basic IF-ELSE program?

I'm quite new to C coding. 我对C编码很陌生。

I have written a basic program for calculating interest (the method for calculating simple interest is wrong but that is not where the current problem lies). 我已经写了一个计算利息的基本程序(计算单利的方法是错误的,但这不是当前问题所在)。 I'm pasting the code here so that you can have a better grasp of the problem. 我将代码粘贴在这里,以便您可以更好地理解问题。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("This is a program which calculates your balance each year for 5 years! \n");

    char name [30];
    float principal;
    float interestRate;
    int interestType;
    int age;

    printf("Start by entering your name \n");
    scanf("%s", name);

    printf("Please enter the type of interest you earn (Simple = 1, Compound = 2) \n");
    scanf("%d", interestType);

    if (interestType == 1)
    {
        printf("You have chosen your interest type as simple \n");

        printf("Please enter your age \n");
        scanf(" %d", &age);

        // IF statement
        if (age < 18)
        {
            printf("You're not eligible for a bank account yet");
        }
        // ELSEIF statement
        else if (age > 122)
        {
            printf("Please submit your age to the Guinness Book of World Records and try again");
        }
        //ELSE statement
        else
        {
            printf("Please enter your current account balance \n");
            scanf(" %f", &principal);

            //Nested IF statement
            if (principal == 0)
            {
                printf("You don't have any balance, please fill money in your account and try again \n");
            }
            else
            {
                printf("Please enter the rate of interest at which your money is being multiplied \n");
                scanf(" %f", &interestRate);

                // principal *= interestRate is the same as principal = principal * interestRate
                principal = principal + interestRate;
                printf("%s's balance after 1 year will be %f \n", name, principal);

                principal = principal + interestRate;
                printf("%s's balance after 2 years will be %f \n", name, principal);

                principal = principal + interestRate;
                printf("%s's balance after 3 years will be %f \n", name, principal);

                principal = principal + interestRate;
                printf("%s's balance after 4 years will be %f \n", name, principal);

                principal = principal + interestRate;
                printf("%s's balance after 5 years will be %f \n", name, principal);

                printf("Thats all");
            }
        }
    }

    else if (interestType == 2)
    {
        printf("You have chosen your interest type as compound \n");

        printf("Please enter your age \n");
        scanf(" %d", &age);

        // IF statement
        if (age < 18)
        {
            printf("You're not eligible for a bank account yet");
        }
        // ELSEIF statement
        else if (age > 122)
        {
            printf("Please submit your age to the Guinness Book of World Records and try again");
        }
        //ELSE statement
        else
        {
            printf("Please enter your current account balance \n");
            scanf(" %f", &principal);

            //Nested IF statement
            if (principal == 0)
            {
                printf("You don't have any balance, please fill money in your account and try again \n");
            }
            else
            {
                printf("Please enter the rate of interest at which your money is being multiplied \n");
                scanf(" %f", &interestRate);

                // principal *= interestRate is the same as principal = principal * interestRate
                principal *= interestRate;
                printf("%s's balance after 1 year will be %f \n", name, principal);

                principal *= interestRate;
                printf("%s's balance after 2 years will be %f \n", name, principal);

                principal *= interestRate;
                printf("%s's balance after 3 years will be %f \n", name, principal);

                principal *= interestRate;
                printf("%s's balance after 4 years will be %f \n", name, principal);

                principal *= interestRate;
                printf("%s's balance after 5 years will be %f \n", name, principal);

                printf("Thats all");
            }
        }
    }

    return 0;
}

The problem I am facing is that whenever the program is run and it asks for the type of interest, after taking the input number (1 or 0) it just stops. 我面临的问题是,每当程序运行并要求输入感兴趣的类型时,在输入数字(1或0)后,它就会停止。

I think you forgot an '&' in scanf("%d", interestType); 我认为您在scanf("%d", interestType);忘记了'&' scanf("%d", interestType); . You need to write it as scanf("%d", &interestType); 您需要将其写为scanf("%d", &interestType); . Also you are checking for input values of 1 and 2. You should put an else clause to handle invalid inputs. 另外,您还要检查输入值1和2。应放置else子句以处理无效输入。 Remaining code is working fine. 其余代码工作正常。

You wrote scanf("%s", name); 您写了scanf("%s", name); but you forgot the & : scanf("%s", &name); 但您忘记了&scanf("%s", &name);

With strings there are other commands, like gets(); 对于字符串,还有其他命令,例如gets(); that you may have not studied yet. 您可能尚未学习。 You should check them, they are pretty useful. 您应该检查它们,它们非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM