6

私は 9 年生なので、まだ C の初心者です。どなたか教えていただけますか? 4 を超える値を入力すると、switch case ステートメントの「default:」ラベルが出力されます。そのために do while を使用しようとしましたが、エラーが発生しました。コードは

#include <stdio.h>
#include <unistd.h>
void main()
{
   int n1,n2,a=0,c,r,o;
S:
   printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
   printf("\n Enter your choice: \t");
   scanf("%d",&o);
   printf("Enter two numbers: \t");
   scanf("%d %d",&n1,&n2);

   switch (o)
   {
      case 1:
         a=n1+n2;
         printf("\n Please wait..");
         sleep(1);
         printf("\n Answer is %d",a);
         printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
         scanf("%d",&c);
         if (c==1)
         {
            goto S;
         }
         if (c==0)
         {
            printf("\n \n \n Bye!");
         }
         else
         {
            printf("Choice ain't correct!");
         }
L:
         printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
         scanf("%d",&r);
         if (r==1)
         {
            printf("\n \n Restarting Loop..");
            sleep(1);
            goto S;
         }
        else
        {
           printf("\n \t \t \t Bye!");
           goto L;
        }
        break;

      case 2:
        a=n1-n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
M:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto M;
        }
        break;

      case 3:
        a=n1*n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
  N:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto N;
        }
        break;

      case 4:
        a=n1/n2;
        printf("\n Please wait..");
        sleep(1);
        printf("\n Answer is %d",a);
        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);
        if (c==1)
        {
           goto S;
        }
        if (c==0)
        {
           printf("\n \n \n Bye!");
        }
        else
        {
           printf("Choice ain't correct!");
        }
O:
        printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
        scanf("%d",&r);
        if (r==1)
        {
           printf("\n \n Restarting Loop..");
           sleep(1);
           goto S;
        }
        else
        {
           printf("\n \t \t \t Bye!");
           goto O;
           break;
      default:
              printf("Choice ain't correct");
              break;
        }
   }
}
4

4 に答える 4

3

あなたのコードは本当に悪いです。投稿のコメントに理由が記載されています。しかし、自分のコードを改善することで学ぶべきだと思います。それが私の初期の頃に役立ったものです。

  1. ユーザーに 2 つの値を入力するように求める前に、(スイッチを使用して) 最初の入力を確認する必要があります。それがプログラムロジックです
  2. デフォルトの場合、goto O; を追加します。またはSに行くことさえあります。

    デフォルト: printf("選択が正しくありません。再試行してください..\n"); 後藤O; 壊す;

  3. さよならを言った後、プログラムを終了したいかもしれません-私にとってはより合理的です

  4. コードをリファクタリングして、go-to なしで実行することを本当にお勧めします。単一のループで解決するのは本当に素晴らしい課題です。

特にどのようなエラーを意味しますか?

〜編集〜

いくつかのコードで私が何を意味するかを説明すると思います。それはあなたのプログラムがどれほど単純かということです.あなたのものよりも維持して読みやすいです;)

#include <stdio.h>
#include <unistd.h>


int main()
{
    int n1,n2,a=0,c,o;

    int terminate = 0;

    while(!terminate)
    {
        printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
        printf("\n Enter your choice: \t");
        scanf("%d",&o);

        if(o < 0 || o> 4)
        {
             printf("Choice ain't correct!\n");
             continue; // restarts loop
        }

        printf("Enter two numbers: \n ");
        scanf("%d %d",&n1,&n2);

        switch(o)
        {
            case 1: a = n1 + n2;
                break;

            case 2: a = n1-n2;
                break;

            case 3: a = n1*n2;
                break;

            case 4: a = n1/n2;
                break;

            default:
                // never reached, since validation of o was done before switch
                break;
        }

        sleep(1);
        printf("\n Answer is %d",a);



        printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
        scanf("%d",&c);

        if (c!=1)
        {
            terminate = 1; // this causes the loop to terminate
        }
    }

    printf("\n \n \n Bye!");

}
于 2013-09-20T11:18:51.423 に答える
-1

default: ケースの直前に右中括弧がありません。それが実行されていない理由です。

于 2013-09-20T11:19:26.623 に答える