3

私はCでプログラムを書いています。プログラムでは、ユーザーは1、2、または3のいずれかの数字を選択する必要があります。ユーザーが1以外の数字を入力すると、 2 または 3 の場合、「無効な選択 - もう一度選択してください」というメッセージが表示され、プログラムの最初に戻ります。

int main() {

    int choice;
    char response, Y, N;

    printf("Choose a shape from the following:\n 1.Sphere\n 2.Cone\n 3.Cylinder\n");

    scanf("%d",&choice);

    if(choice==1||choice==2||choice==3) {
        printf("Enter the radius, r\n");                             
    } else
        printf("Invalid selection, choose again.\n");

}

私が望むのは、「無効な選択、もう一度選択してください」が表示された後、ユーザーがプログラムの最初に戻されるので、選択を再度入力できることです。どうすればいいですか?

4

6 に答える 6

2

これがあなたがすることです:

int choice;
char response, Y, N;
for (;;) {
    printf("Choose a shape from the following:\n 1.Sphere\n 2.Cone\n 3.Cylinder\n");

    scanf("%d",&choice);

    if(choice==1||choice==2||choice==3) {
        break;                    
    }
    printf("Invalid selection, choose again.\n");
}

このループが終わったら、半径を入力してください。半径に負の値が入力されるのを防ぐために、ほぼ確実に別のループが必要になるため、同じループでプロンプトを出さないでください。

于 2012-10-21T13:37:47.887 に答える
1

これにはwhileループを使用します

int main()
{
    int choice;
    char response, Y, N;

    printf("Choose a shape from the following:\n 1.Sphere\n 2.Cone\n 3.Cylinder\n");
    while(1)
    {
        scanf("%d",&choice);

        if(choice==1||choice==2||choice==3)
        {
            printf("Enter the radius, r\n");                             
            //Maybe read the radius here
            scanf("%d",&radius);
            break;
        }
        else
            printf("Invalid selection, choose again.\n");
    }
}
于 2012-10-21T13:33:35.657 に答える
0

do-whileループを使用し、正しい入力
がこのようになるまでループし、終了の選択肢 4 を追加します。

 do {
        scanf("%d",&choice);
int flag=0;
        if(choice==1||choice==2||choice==3) {
            printf("Enter the radius, r\n");                             
        } else {
            printf("Invalid selection, choose again.\n");
            flag=1;
        }
    } while(flag==1&& choice!=4);
于 2012-10-21T13:33:25.100 に答える
0

これに異議を唱える人もいますが、これらの状況でgotoは、「通常の」制御パスの直線性を強調するので、単純な古い方法が非常にクリーンで読みやすい方法だと思います。

int main(int arg, char *argv[])
{
    int choice;

choose_shape:
    printf("Choose a shape from the following:\n"
           " 1.Sphere\n 2.Cone\n 3.Cylinder\n");
    scanf("%d", &choice);
    if (choice < 1 || choice > 3) {
        printf("Invalid selection, please choose again.\n");
        goto choose_shape;
    }

    printf("Enter the radius, r:\n");
    ...
}

はい、人々は不平を言っているgotoので、もう少し正当化させてください。

これは、文字で形状を選択できる、より洗練されたバージョンです。

    char c;
    shape_t shape;
choose_shape:
    printf("Choose a shape: [s]phere, [c]one, c[y]linder:\n");
    scanf("%c", &c);
    switch (c) {
    cases 's':
        shape = SHAPE_SPHERE;
        break;

    case 'c':
        shape = SHAPE_CONE;
        break;

    case 'y':
        shape = SHAPE_CYLINDER;
        break;

    default:
        printf("Not a valid shape: %c\n", c);
        goto choose_shape;
    }

そして、これが 付きのバージョンgotoです。これにより別の変数 が導入されることに注意してください。この変数の唯一の目的は、ステートメントflagを取り除くことです。ステートメントのため、ここ (最初はラベルなし) をgoto単純に使用することはできません。状態が追加されているため、これは読みにくいと思います。5行長くなります。breakgotoswitch

    char c;
    shape_t shape;
    int flag;
    for (flag = 0; !flag; ) {
        printf("Choose a shape: [s]phere, [c]one, c[y]linder:\n");
        scanf("%c", &c);
        switch (c) {
        cases 's':
            shape = SHAPE_SPHERE;
            flag = 1;
            break;

        case 'c':
            shape = SHAPE_CONE;
            flag = 1;
            break;

        case 'y':
            shape = SHAPE_CYLINDER;
            flag = 1;
            break;

        default:
            printf("Not a valid shape: %c\n", c);
            break;
        }
    }
于 2012-10-21T13:34:15.740 に答える
0

検討すべき代替案を次に示します。ユーザー入力の取得は関数に分割され、main()はその関数を呼び出してエラー時にループすることに注意してください。の読者はmain()、入力の選択肢がどのように得られるかなど気にしないでしょう。

fgetsまた、 ではなくを使用していることにも注意してくださいscanf。バージョンを実行してscanf数字以外を入力すると、その文字は入力バッファに無期限に残ります。scanf%d フォーマット文字列を満たす数字を探しているため、絶対に削除されません。したがって、無限ループになります。( を使用して)stdinの前にフラッシュを試みることはできますが、関数は のクローズを正しく処理しません (たとえば、シェル UNIX ベースのシステムで ctrl-d を使用)。scanffpurgestdin

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

static int get_shape(int *shape)
{
    char buf[10] = "";
    printf("Choose a shape from the following:\n"
           " 1.Sphere\n 2.Cone\n 3.Cylinder\n");

    if (!fgets(buf, sizeof buf, stdin)) { /* Note: fgets, not scanf */
        exit(1); /* ctrl-d */
    }
    *shape = strtol(buf, NULL, 0);

    if (*shape==1 || *shape==2 || *shape==3) {
        return 1;
    }
    printf("Invalid selection\n");
    return 0;
}

int main(int argc, char ** argv)
{
    int shape = 0;
    while (!get_shape(&shape)) {
        /* loop */
    }
    printf("Choice: %d\n", shape);
    return 0;
}
于 2012-10-21T15:50:09.860 に答える