2

だから、私が作っているこの計算機(まだCを学んでいます)に問題があり続けて、コードの最後に「Expected }」というエラーが表示されます。しかし、最後に}があり、それが機能するかどうかを確認するために別のものを追加しましたが、「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

2
else if(number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);

このelse ifステートメントを閉じることはありません。

于 2013-06-28T17:04:33.320 に答える
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 に答える