2

所以,我正在制作的这个计算器一直有问题(仍在学习 C),它在代码的最后给我一个错误,“预期 }”。但我最后有 },我添加了另一个,看看是否可行,但随后显示“在 do/while 循环中预期”。

#include <stdio.h>
#include <stdbool.h>

int main()
{
    float number[100];
    int operator = '0';
    int doAgainAnswer = 0;
    int doAgain;
    float finished;
    int error;
    do{
        if(number[1] == 0){
            printf("Number 1: ");
            scanf("%f", &number[1]);
        } else if(number[1] != 0) {
            printf("\n\nNumber 1: %.2f\n", number[1]);


            printf("Number 2: ");
            scanf("%f", &number[2]);
            printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
            scanf("%d", &operator);

            if(operator == 1){
                finished = number[1] + number[2];
                printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);

            }
            if(operator == 2){
                finished = number[1] - number[2];
                printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);

            }
            if(operator == 3){
                finished = number[1] * number[2];
                printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);

            }
            if(operator == 4){
                finished = number[1] / number[2];
                printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
            }

            if(operator > 4){
                printf("\n\nERROR: Invalid operation.\n\n ");
                error = '1';
            }
            if(error != 1){
                printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
            } else if(error == 1){
                printf("\nTry again? 1 Yes 3 No: ");
            }
            scanf("%d", &doAgainAnswer);
            if(doAgainAnswer == 1) {
                doAgain = 1;
            } else

                if(doAgainAnswer == 2){
                    doAgain = 1;
                    number[1] = finished;

                } else
                    if(doAgainAnswer == 3){
                        doAgain = 0;
                        printf("Goodbye :(");
                        exit(0);
                    } else
                        if(doAgainAnswer > 4){
                            printf("\n\nERROR INVALID OPERATION.\n\n");
                        }
        }while(doAgain == 1);
        return 0;

    }
4

6 回答 6

3

看起来您的这部分代码包含一个else永远不会关闭的内容,即:

 if(number[1] == 0)
 {
    printf("Number 1: ");
    scanf("%f", &number[1]);
 } 
 else if(number[1] != 0) 
 {
   ....
 } <-- missing.

整个代码块,重新格式化略有不同:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    float number[100];
    int operator = '0';
    int doAgainAnswer = 0;
    int doAgain;
    float finished;
    int error;
    do
    {
        if(number[1] == 0)
        {
            printf("Number 1: ");
            scanf("%f", &number[1]);
        } 
        else if(number[1] != 0) 
        {
            printf("\n\nNumber 1: %.2f\n", number[1]);            
            printf("Number 2: ");
            scanf("%f", &number[2]);
            printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
            scanf("%d", &operator);

            if(operator == 1)
            {
                finished = number[1] + number[2];
                printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
            }

            if(operator == 2)
            {
                finished = number[1] - number[2];
                printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);                
            }

            if(operator == 3)
            {
                finished = number[1] * number[2];
                printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);                
            }

            if(operator == 4)
            {
                finished = number[1] / number[2];
                printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
            }

            if(operator > 4)
            {
                printf("\n\nERROR: Invalid operation.\n\n ");
                error = '1';
            }

            if(error != 1)
            {
                printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
            } 
            else if(error == 1)
            {
                printf("\nTry again? 1 Yes 3 No: ");
            }

            scanf("%d", &doAgainAnswer);
            if(doAgainAnswer == 1) 
            {
                doAgain = 1;
            } 
            else if(doAgainAnswer == 2)
            {
                doAgain = 1;
                number[1] = finished;
            } 
            else if(doAgainAnswer == 3)
            {
                doAgain = 0;
                printf("Goodbye :(");
                exit(0);
            } 
            else if(doAgainAnswer > 4)
            {
                printf("\n\nERROR INVALID OPERATION.\n\n");
            }
        }
    }
    while(doAgain == 1);
    return 0;

}
于 2013-06-28T17:06:39.583 回答
2
else if(number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);

你永远不会关闭这个 else if 语句。

于 2013-06-28T17:04:33.320 回答
1

为防止发生此类错误,我建议您使用不同类型的缩进。

如果您这样编写代码:

int main()
{
    float number[100];
    int operator = '0';
    int doAgainAnswer = 0;
    int doAgain;
    float finished;
    int error;
    do
    {
        if(number[1] == 0)
        {
           printf("Number 1: ");
           scanf("%f", &number[1]);
        }
        else if(number[1] != 0)
        {
            printf("\n\nNumber 1: %.2f\n", number[1]);

        printf("Number 2: ");
        scanf("%f", &number[2]);
        printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
        scanf("%d", &operator);

您应该立即注意到语句}后面有一个缺失。else if

于 2013-06-28T17:06:57.633 回答
1

尝试在换行符中编写 {} 并在该行中没有代码。它将帮助您组织起来并更轻松地发现问题!

于 2013-06-28T17:08:03.190 回答
1

正确的缩进表明您缺少 a}以匹配您的第一个else if {.

您确实需要使用适当的编辑器(至少可以自动缩进的编辑器),{}在您else的 s 周围使用以避免悬挂它们*,并使用辅助函数。

#include <stdio.h>
#include <stdbool.h>
int main()
{
    ...
    do{
        if(number[1] == 0){
            printf("Number 1: ");
            scanf("%f", &number[1]);
        } else if(number[1] != 0) {
            ...
        }while(doAgain == 1);
        return 0;

    }

*:严肃地说,除非它足够短,可以放在一行(或类似的)上,否则请始终使用大括号。else return我在上一家公司的代码中发现了这一点:

if(DEBUG) 
//    print(foo);

if(something_important) {
    ...
}
于 2013-06-28T17:10:32.783 回答
1

您的代码需要更好的缩进。通过适当的缩进,可以更容易地发现缺少括号的位置是(或不是)。您没有if/else在条件之前关闭外部语句while

#include <stdio.h>
#include <stdbool.h>

int main()
{
    float number[100];
    int operator = '0';
    int doAgainAnswer = 0;
    int doAgain;
    float finished;
    int error;

    do {
        if(number[1] == 0) {
            printf("Number 1: ");
            scanf("%f", &number[1]);
        }
        else if (number[1] != 0) {
            printf("\n\nNumber 1: %.2f\n", number[1]);
            printf("Number 2: ");
            scanf("%f", &number[2]);
            printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
            scanf("%d", &operator);

            if (operator == 1) {
                finished = number[1] + number[2];
                printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
            }

            if (operator == 2) {
                finished = number[1] - number[2];
                printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);
            }

            if (operator == 3) {
            finished = number[1] * number[2];
            printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);
            }

            if (operator == 4) {
                finished = number[1] / number[2];
                printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
            }

            if (operator > 4) {
                printf("\n\nERROR: Invalid operation.\n\n ");
                error = '1';
            }

            if (error != 1) {
                printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
            }
            else if (error == 1) {
                printf("\nTry again? 1 Yes 3 No: ");
            }

            scanf("%d", &doAgainAnswer);

            if (doAgainAnswer == 1) {
                doAgain = 1;
            }
            else if(doAgainAnswer == 2) {
                doAgain = 1;
                number[1] = finished;
            }
            else if (doAgainAnswer == 3) {
                doAgain = 0;
                printf("Goodbye :(");
                exit(0);
            }
            else if (doAgainAnswer > 4) {
                printf("\n\nERROR INVALID OPERATION.\n\n");
            }
    /**
     * Missing a bracket here, to close the if/else statement
     */
        }
        while (doAgain == 1);

        return 0;
    }
// Indentation should end at this level
于 2013-06-28T17:25:25.160 回答