0

「私が抱えている実際の問題は、質問の数を出した後に実行が停止することです。「%d%」を「%d」に修正しましたが、まだその問題があります」

私は現在、小さな数学ゲームを作成するプロジェクトに取り組んでいます。難易度 (Easy、Hard、または Quit) レベルとそれに続くカテゴリ (加算、減算、乗算、および除算) を求めるプロンプトが必要です。

printf (" How many questions would you like to attempt?\n");
scanf ("%d%", &numofquestions);

仕事に。誰かが私が間違っていることを教えてもらえますか? 以下はこれまでの私の完全なコードです。不要な項目がいくつかありますが、私の知る限り、それらは問題ではありません。

/* Program Description Goes Here */
#include <stdio.h>
#include <stdlib.h>

 /* function main begins program execution */
int main( void )
{
    /* Main Code Area For Program */
int difficulty = -1;
int category = 0;
int numofquestions;
int ca = 0; /* Correct Answer */
int cc; /* Correcnt Counter */
int ran1;
int ran2;
int random1 = 0;
int random2 = 0;
int usera; 
int i;
/* Welcome mesage */
srand ( time (NULL));

printf ( "Welcome to the math challenge.\n"); 


while ( difficulty != 1 && difficulty != 2 && difficulty !=0){ 

    printf ("Select 1 for Easy\n");
    printf ("Select 2 for Hard\n");
    printf ("Select 0 to Quit\n");
    scanf ( "%d", &difficulty);
}

        while ( category != 1 && category != 2 && category != 3 && category != 4){

    category = getchar();
    switch (category){
        case 'A':
        case 'a':
            category = 1;
            break;
        case 'S':
        case 's':
            category = 2;
            break;
        case 'M':
        case 'm':
            category = 3;
            break;
        case 'D':
        case 'd':
            category = 4;
            break;
        case '/n':
        case '/t':
        case ' ':
            break;
        default:
            printf ("Enter A for Addition\n");
            printf ("Enter S for Subtraction\n");
            printf ("Enter M for Multiplication\n");
            printf ("Enter D for Division\n");
    }
}

        printf (" How many questions would you like to attempt?\n");
        scanf ("%d%", &numofquestions);


        for (i = numofquestions; i<=0; --i){/* start for */

            if ( difficulty == 1){ /* start if */
                random1 = (rand () % 10);
                random2 = (rand () % 10);

                }/* end if */ 

            if ( category == 1){/* start if */
                printf ( "%d + %d = ?" , random1 , random2 );
                ca = random1 + random2;
                printf ( "Your response\n");
                scanf ( "%d", &usera);
                                }/* end if */

            if ( usera == ca ){ /* start if */
                printf (" %d\n ", ca);
            }/* end if */


        /* end for */}


return 0; /* indicate that program ended successfully */
/* end function main */
}
4

3 に答える 3

1

次の重複を削除%します。

scanf("%d%", &numofquestions);

次のようにする必要があります。

scanf("%d", &numofquestions);
于 2013-03-21T17:38:49.977 に答える
1

その余分な %d* % * は意図的ですか?

scanf ("%d%", &numofquestions);

for (i = numofquestions; i<=0; --i){/* start for */ これは for (i = numofquestions; i>=0; --i) だと思います!

于 2013-03-21T17:40:54.733 に答える
0

getchar()int を返し、switch ステートメントでチェック文字を返します。http://www.cplusplus.com/reference/cstdio/getchar/

%d%する必要があります%dfor (i = numofquestions; i<=0; --i){あなたはintを取得しています。これは数値が 0 以下の場合にのみ実行され、その後は永久に実行されます。

于 2014-10-10T20:23:28.090 に答える